2010-11-11 68 views
0

我試圖在默認瀏覽器中打開一個url。很明顯,我認爲Shell Exec會在默認瀏覽器中打開它,但它不會。註冊表中的DefaultBrowser不起作用

然後我試圖明確:

Process.Start(GetDefaultBrowserPath(), "http://stackoverflow.com"); 

private static string GetDefaultBrowserPath() 
{ 
    string key = @"htmlfile\shell\open\command"; 
    RegistryKey registryKey = 
    Registry.ClassesRoot.OpenSubKey(key, false); 
    // get default browser path 
    return ((string)registryKey.GetValue(null, null)).Split('"')[1]; 
} 

它總是返回的Internet Explorer但沒有哪是Firefox的我的默認。我試了幾臺電腦...

我不在乎來調用默認瀏覽器鏈接哪種方式,但它是默認

+0

當你說這是你的默認,你確定,如果您創建一個新的.html文件在你的桌面上,雙擊它,它實際上在Firefox中打開? – Jeff 2010-11-11 14:30:28

+0

是的,我只是試過。桌面上的新.html文件,雙擊並在Firefox中打開。 – Kai 2010-11-11 14:32:18

+0

'Process.Start(「http://stackoverflow.com」);'在我的機器上運行(打開Chrome)。 – dtb 2010-11-11 14:35:27

回答

4

您是否嘗試過只是運行:

Process.Start("http://stackoverflow.com"); 

我的測試應用(下)打開我的默認瀏覽器的網站:

using System; 
using System.Diagnostics; 

namespace ProcessStartSample 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Process.Start("http://stackoverflow.com"); 
     } 
    } 
} 

換句話說,讓操作小號ystem在做出用戶默認瀏覽器爲您服務的繁重工作! =)

0

Windows會自動啓動用戶的系統上的默認瀏覽器爲你,如果你只是指定的URL打開:

Process.Start("http://www.google.com/"); 

無需任何花哨的註冊弄虛作假,除非你正試圖瞭解其中瀏覽器被設置爲默認值。

+0

我曾嘗試過。相同 - 仍然打開IE – Kai 2010-11-11 15:23:31

1

就試試這個:)

Process.Start("http://stackoverflow.com"); 

如果你想找到你的默認瀏覽器,你應該打開HKEY_CLASSES_ROOT\http\shell\open\command\default關鍵。

請注意 「HTTP」 而不是 「HTMLFILE」

編輯:

CODE:

RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command", false); 
string value = registryKey.GetValue("").ToString(); 
+0

RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(「\ http \ shell \ open \ command \ default」,false); 但現在registryKey爲空 – Kai 2010-11-11 15:25:31

+0

請檢查編輯的代碼。 – Barun 2010-11-11 15:44:30