2009-10-13 4 views
3

J'utilise la fonction ci-dessous pour ouvrir le navigateur Web par défaut de l'utilisateur.Ouverture du navigateur Web par défaut

Public Function ShowHelp(ByVal url As String) As System.Diagnostics.Process 
    Dim startInfo As New Diagnostics.ProcessStartInfo() 
    startInfo.FileName = url 
    startInfo.WindowStyle = ProcessWindowStyle.Maximized 
    Return System.Diagnostics.Process.Start(startInfo) 
End Function 

Deux ou trois fois la fonction a renvoyé l'erreur (sur la machine des utilisateurs) « le système ne peut pas trouver le fichier spécifié »

je suppose que l'utilisateur a défini pas un navigateur Web par défaut. Pourquoi j'ai cette erreur? Comment puis-je ajouter une vérification de navigateur Web par défaut avant d'appeler cette fonction?

+0

Etes-vous sûr de son navigateur par défaut? –

+0

Pardonnez-moi? Je ne comprends pas votre commentaire – OrElse

Répondre

0

C'est en C#, mais cela est un bon article:

http://ryanfarley.com/blog/archive/2004/05/16/649.aspx

Voici le C# comme VB.NET:

Private Function getDefaultBrowser() As String 
    Dim browser As String = String.Empty 
    Dim key As RegistryKey = Nothing 
    Try 
     key = Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command", False) 

     'trim off quotes 
     browser = key.GetValue(Nothing).ToString().ToLower().Replace("""", "") 
     If Not browser.EndsWith("exe") Then 
      'get rid of everything after the ".exe" 
      browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4) 
     End If 
    Finally 
     If key IsNot Nothing Then 
      key.Close() 
     End If 
    End Try 
    Return browser 
End Function 
1

C'est la bonne façon de lancer le navigateur avec une URL en général, mais si elle échoue, je voudrais juste attraper cette exception spécifique, puis tenter d'appeler iexplore <url> pour ouvrir l'URL dans IE, étant donné qu'il est bo et être installé sur n'importe quel système Windows. (Je suppose que vous n'êtes pas cibler Mono/Linux ici.)

+0

Divers cas juridiques ont forcé Microsoft à créer un certain nombre de versions de Windows sans IE - vous ne pouvez pas supposer qu'il est présent. – MarkJ

+0

@MarkJ: Ah oui, je m'en souviens maintenant. Pourtant, plus souvent qu'autrement, il existe. Un juste dernier recours, à mon avis. – Noldorin

0

Si vous utilisez Windows, la ligne de commande suivante devrait fonctionner à partir de n'importe où:

rundll32 url.dll,FileProtocolHandler <your_url> 

où < your_url> est la page Web URL vers laquelle naviguer

Public Function ShowHelp(ByVal url As String) As System.Diagnostics.Process 
     Dim startInfo As New Diagnostics.ProcessStartInfo() 
     startInfo.FileName = "rundll32 url.dll,FileProtocolHandler" 
     startInfo.Arguments = url 
     startInfo.WindowStyle = ProcessWindowStyle.Maximized 
     Return System.Diagnostics.Process.Start(startInfo) 
End Function 
0

Si vous souhaitez dislay un fichier les extrémités dans « .html » ou « htm », vous pouvez alors juste passer à la méthode Process.Start(). La même chose peut fonctionner avec une URL.

(Vous devez avoir le drapeau qui obtient Process.Start() pour utiliser les méthodes shell.)

2
Private Sub helpRichTextBox_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs) Handles helpRichTextBox.LinkClicked 
     System.Diagnostics.Process.Start(e.LinkText) 
    End Sub 
+0

La seule bonne réponse – abatishchev

Questions connexes