2014/02/17

WPF 透過RegistryKey取得預設瀏覽器

Button1是對岸提供的機碼,Button2也是參考對岸的機碼
C#.Net適用

using System;
using System.Windows;
using Microsoft.Win32;

namespace GetProcessForm
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice");
            Console.WriteLine(key.GetValue("Progid").ToString());
            key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice");
            Console.WriteLine(key.GetValue("Progid").ToString());
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
            string s = key.GetValue("").ToString();
            string str = s.Substring(s.IndexOf(":") - 1, s.IndexOf(".exe") + ".exe".Length - s.IndexOf(":") + 1);
            Console.WriteLine(s);
        }
    }
}


參考資料:
http://bbs.csdn.net/topics/390401279?page=1#post-394000214
http://superuser.com/questions/368814/how-do-i-change-my-default-browser-to-an-unlisted-program-in-windows-7