質問

どのRegKeyからデフォルトのブラウザアプリケーションのパスを取得できますか?

C#/。NETからアクセスする最良の方法は?

役に立ちましたか?

解決

必要なキーは次のとおりです。

  

HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes \ http \ shell \ open \ command

また、必要に応じて、 C#のレジストリチュートリアルを簡単に紹介します。

編集

ユーザーごとの設定には、次のキーを使用します:

  

HKEY_CLASSES_ROOT \ http \ shell \ open \ command

(HKCRにはマシンとユーザーの両方の設定があり、ユーザーが優先されます)。

Vistaでは動作しない可能性があることに注意してください。詳細については、こちらをご覧ください。

他のヒント

Windows 7の場合、デフォルトのブラウザパスは次のレジストリキーに保存

 HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\ Associations\UrlAssociations\http

c#を使用すると、次のように取得できます-

RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false);

string browser = regkey.GetValue("Progid").ToString();

あなたの答えに基づいて、私はあなたが望むことをするはずのこのサンプルコードを書きました(テストされていません)

public static string GetDefaultBrowserPath()
    {
        string defaultBrowserPath = null;
        RegistryKey regkey;

        // Check if we are on Vista or Higher
        OperatingSystem OS = Environment.OSVersion;
        if ((OS.Platform == PlatformID.Win32NT) && (OS.Version.Major >= 6))
        {
            regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false);
            if (regkey != null)
            {
                defaultBrowserPath = regkey.GetValue("Progid").ToString();
            }
            else
            {
                regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes\\IE.HTTP\\shell\\open\\command", false);
                defaultBrowserPath = regkey.GetValue("").ToString();
            }
        }
        else
        {
            regkey = Registry.ClassesRoot.OpenSubKey("http\\shell\\open\\command", false);
            defaultBrowserPath = regkey.GetValue("").ToString();
        }

        return defaultBrowserPath;
    }

このための関数を作成しました:

    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));
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top