2010-11-11 74 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