如何使用C#查找默认Web浏览器?

Hop*_*e S 32 c#

有没有办法可以使用C#查找我的默认Web浏览器的名称?(Firefox,谷歌浏览器等..)

能告诉我一个例子吗?

Ste*_*ris 45

当Internet Explorer设置为默认浏览器时,当前接受的答案对我不起作用.在我的Windows 7 PC上HKEY_CLASSES_ROOT\http\shell\open\command,IE没有更新.这背后的原因可能是从Windows Vista开始引入的默认程序处理方式的变化.

您可以在注册表项中找到默认选择的浏览器 Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice,并带有值Progid.(感谢Broken Pixels)

const string userChoice = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice";
string progId;
BrowserApplication browser;
using ( RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey( userChoice ) )
{
    if ( userChoiceKey == null )
    {
        browser = BrowserApplication.Unknown;
        break;
    }
    object progIdValue = userChoiceKey.GetValue( "Progid" );
    if ( progIdValue == null )
    {
        browser = BrowserApplication.Unknown;
        break;
    }
    progId = progIdValue.ToString();
    switch ( progId )
    {
        case "IE.HTTP":
            browser = BrowserApplication.InternetExplorer;
            break;
        case "FirefoxURL":
            browser = BrowserApplication.Firefox;
            break;
        case "ChromeHTML":
            browser = BrowserApplication.Chrome;
            break;
        case "OperaStable":
            browser = BrowserApplication.Opera;
            break;
        case "SafariHTML":
            browser = BrowserApplication.Safari;
            break;
        case "AppXq0fevzme2pys62n3e0fbqa7peapykr8v":
            browser = BrowserApplication.Edge;
            break;
        default:
            browser = BrowserApplication.Unknown;
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您还需要浏览器可执行文件的路径,您可以按如下方式访问它,使用Progid从中检索它ClassesRoot.

const string exeSuffix = ".exe";
string path = progId + @"\shell\open\command";
FileInfo browserPath;
using ( RegistryKey pathKey = Registry.ClassesRoot.OpenSubKey( path ) )
{
    if ( pathKey == null )
    {
        return;
    }

    // Trim parameters.
    try
    {
        path = pathKey.GetValue( null ).ToString().ToLower().Replace( "\"", "" );
        if ( !path.EndsWith( exeSuffix ) )
        {
            path = path.Substring( 0, path.LastIndexOf( exeSuffix, StringComparison.Ordinal ) + exeSuffix.Length );
            browserPath = new FileInfo( path );
        }
    }
    catch
    {
        // Assume the registry value is set incorrectly, or some funky browser is used which currently is unknown.
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 但就我个人而言,我认为比检索可执行路径更优雅的是,只需在检索到的字符串中填写 %1 参数,将其拆分为程序和参数,然后按照注册表中的定义执行它。 (2认同)

Mar*_*num 22

你可以在这里看一个例子,但主要可以这样做:

internal string GetSystemDefaultBrowser()
{
    string name = string.Empty;
    RegistryKey regKey = null;

    try
    {
        //set the registry key we want to open
        regKey = Registry.ClassesRoot.OpenSubKey("HTTP\\shell\\open\\command", false);

        //get rid of the enclosing quotes
        name = regKey.GetValue(null).ToString().ToLower().Replace("" + (char)34, "");

        //check to see if the value ends with .exe (this way we can remove any command line arguments)
        if (!name.EndsWith("exe"))
            //get rid of all command line arguments (anything after the .exe must go)
            name = name.Substring(0, name.LastIndexOf(".exe") + 4);

    }
    catch (Exception ex)
    {
        name = string.Format("ERROR: An exception of type: {0} occurred in method: {1} in the following module: {2}", ex.GetType(), ex.TargetSite, this.GetType());
    }
    finally
    {
        //check and see if the key is still open, if so
        //then close it
        if (regKey != null)
            regKey.Close();
    }
    //return the value
    return name;

}
Run Code Online (Sandbox Code Playgroud)

  • 此方法似乎不再起作用. (3认同)
  • 在我的Win 8.1机器上,这种技术可以启动Firefox(附带的消息是Firefox不是默认的浏览器).这应该是回归(对于较旧的操作系统)到Jeuris的回答(需要一些编辑工作). (3认同)
  • 当我将Internet Explorer设置为默认浏览器时,它似乎对我不起作用.:/该特定注册表位置未更新为指向IE. (2认同)

小智 10

我刚刚为此做了一个函数:

    public void launchBrowser(string url)
    {
        string browserName = "iexplore.exe";
        using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"))
        {
            if (userChoiceKey != null)
            {
                object progIdValue = userChoiceKey.GetValue("Progid");
                if (progIdValue != null)
                {
                    if(progIdValue.ToString().ToLower().Contains("chrome"))
                        browserName = "chrome.exe";
                    else if(progIdValue.ToString().ToLower().Contains("firefox"))
                        browserName = "firefox.exe";
                    else if (progIdValue.ToString().ToLower().Contains("safari"))
                        browserName = "safari.exe";
                    else if (progIdValue.ToString().ToLower().Contains("opera"))
                        browserName = "opera.exe";
                }
            }
        }

        Process.Start(new ProcessStartInfo(browserName, url));
    }
Run Code Online (Sandbox Code Playgroud)


小智 6

这已经过时了,但我只会添加我自己的发现供其他人使用。的值HKEY_CURRENT_USER\Software\Clients\StartMenuInternet应该为您提供此用户的默认浏览器名称。

如果要枚举所有已安装的浏览器,请使用 HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet

您可以使用找到的名称HKEY_CURRENT_USER来标识默认浏览器HKEY_LOCAL_MACHINE,然后以这种方式找到路径。


小智 5

视窗10

internal string GetSystemDefaultBrowser()
    {
        string name = string.Empty;
        RegistryKey regKey = null;

        try
        {
            var regDefault = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.htm\\UserChoice", false);
            var stringDefault = regDefault.GetValue("ProgId");

            regKey = Registry.ClassesRoot.OpenSubKey(stringDefault + "\\shell\\open\\command", false);
            name = regKey.GetValue(null).ToString().ToLower().Replace("" + (char)34, "");

            if (!name.EndsWith("exe"))
                name = name.Substring(0, name.LastIndexOf(".exe") + 4);

        }
        catch (Exception ex)
        {
            name = string.Format("ERROR: An exception of type: {0} occurred in method: {1} in the following module: {2}", ex.GetType(), ex.TargetSite, this.GetType());
        }
        finally
        {
            if (regKey != null)
                regKey.Close();
        }

        return name;
    }
Run Code Online (Sandbox Code Playgroud)