web-dev-qa-db-fra.com

Ouvrir un fichier html à l'aide du navigateur Web par défaut

J'utilise ceci pour obtenir le chemin et l'exécutable du navigateur Web par défaut:

public static string DefaultWebBrowser
        {
            get
            {

                string path = @"\http\Shell\open\command";

                using (RegistryKey reg = Registry.ClassesRoot.OpenSubKey(path))
                {
                    if (reg != null)
                    {
                        string webBrowserPath = reg.GetValue(String.Empty) as string;

                        if (!String.IsNullOrEmpty(webBrowserPath))
                        {
                            if (webBrowserPath.First() == '"')
                            {
                                return webBrowserPath.Split('"')[1];
                            }

                            return webBrowserPath.Split(' ')[0];
                        }
                    }

                    return null;
                }
            }
        }

Et:

 protected static bool Run(string FileName, string Args)
        {
            try
            {
                Process proc = new Process();

                processInfo.FileName = FileName;
                 proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

                if(Args != null) proc.StartInfo.Arguments = Args;

                proc.Start();

                return true;
            }
            catch (Exception) { }

            return false;
        }

J'appelle ensuite le navigateur Web: Run(DefaultWebBrowser, "foo.html")

La question est: la fonction ci-dessus appelle Firefox et IE (les deux navigateurs Web installés sur mon ordinateur) au lieu d’Internet Explorer, le navigateur Web par défaut. Je ne sais pas comment résoudre ce problème.

MODIFIER

J'ai téléchargé et installé Google Chrome, définissez-le comme navigateur Web par défaut, mais curieusement, l'erreur ci-dessus ne se produit pas.

12
Jack

Vous pouvez remplacer tout ce code par

System.Diagnostics.Process.Start(pathToHtmlFile);

Cela lancera automatiquement votre navigateur par défaut, ou plutôt recherchera le gestionnaire par défaut pour les fichiers .htm ou .html et l’utilisera.

Maintenant, avec Firefox défini par défaut, cela peut parfois provoquer d’exceptions étranges (je pense que si Firefox démarre pour la première fois), vous pouvez donc créer un try/catch pour le gérer.

29
Darko Z

Pour ceux qui n'ont pas d'association HTML par défaut à un navigateur, utilisez

System.Diagnostics.Process.Start("Chrome", Uri.EscapeDataString(pathToHtmlFile))

1
Richard Matsen

J'utilise un code, où je cherche d'abord les fichiers exe. Par exemple, si exsists chrome.exe (dans son chemin par défaut) else sinon existe firefox.exe ou launcher.exe (pour l'opera) etc ... si aucune n'existe, essayez d'exécuter iexplore.exe avec le paramètre pathToHtmlFile. Ceci est ma solution, où j'utilise une configuration externe, où définir mon navigateur, peu importe ce qui est défini par défaut dans le système d'exploitation.

0
Dávid Miškovič