我使用以下代码在Windows 8.1的系统默认浏览器中成功打开请求的URL:
public static void OpenUrlInDefaultBrowser(string url)
{
var httpKey = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command");
if (httpKey == null || httpKey.GetValue(string.Empty) == null)
return;
var cmd = httpKey.GetValue(string.Empty) as string;
if (cmd != null)
{
try
{
if (cmd.Length > 0)
{
string[] splitStr;
string fileName;
string args;
if (cmd.Substring(0, 1) == "\"")
{
splitStr = cmd.Split(new[] { "\" " }, StringSplitOptions.None);
fileName = splitStr[0] + "\"";
args = cmd.Substring(splitStr[0].Length + 2);
}
else
{
splitStr = cmd.Split(new[] { " " }, StringSplitOptions.None);
fileName = splitStr[0];
args = cmd.Substring(splitStr[0].Length + 1);
}
System.Diagnostics.Process.Start(fileName, args.Replace("%1", url));
}
}
catch (Exception)
{
// handle exception
}
}
httpKey.Close();
}
但是,在我的Windows Server 2008 R2 VM上,相同的代码会打开Internet Explorer(该计算机上的默认设置),但只会加载URL res://iesetup.dll/SoftAdmin.htm
。 IE设置为关闭增强安全模式。 Chrome在此计算机上按预期工作。
简单地调用Process.Start(url)
也无法打开请求的网址。
当我从" Run ..."执行以下操作时菜单,它按预期工作:"C:\Program Files\Internet Explorer\iexplore.exe" http://example.com
。 PowerShell中的start-process http://example.com
也是如此。
答案 0 :(得分:1)
您是否尝试过调用Process.Start("http://your_website_here");
?如果你想在默认的情况下运行超链接,那就不像你需要指定浏览器了。
@davidsbro - 是的,我不会把它作为答案否则:) @mark - 尝试http://blog.blksthl.com/2012/11/28/how-to-disable-ie-enhanced-security-in-windows-server-2012/ - 这与服务器安全设置而非应用程序有关。
答案 1 :(得分:0)
我写了一个上面方法的修改版本来启动,即使用命令行选项,并且在确定要启动哪个过程时更加详细。
此外,尝试更改代码我发布的Process.Start调用以使用特定用户和密码。
class Program
{
static void Main(string[] args)
{
OpenUrlInBrowser("http://www.stackoverflow.com");
}
public static string GetDefaultBrowserCommand()
{
string command = null;
var httpKey = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command");
if (httpKey == null)
throw new Exception("No default browser is configured!");
else
{
command = (string)httpKey.GetValue("", "iexplore.exe");
int exeIndex = command.ToLower().IndexOf(".exe");
if (exeIndex > -1)
{
int endOfCommand = command.IndexOf('"', exeIndex);
int startOfCommand = command.LastIndexOf('"', exeIndex - 1);
if (startOfCommand > -1 && endOfCommand > -1)
{
command = command.Substring(startOfCommand + 1, endOfCommand - 1);
return command;
}
else
throw new Exception("Error: Default browser is not set in the registry correctly!");
}
else
throw new Exception("The default browser registry setting does not specify an executable!");
}
}
public static void OpenUrlInBrowser(string url)
{
string browserCommand = GetDefaultBrowserCommand();
FileInfo fi = new FileInfo(browserCommand);
ProcessStartInfo psi = null;
if (!fi.Exists)
Console.WriteLine("The default browser specified in the registry does not physical exist on disk!");
else
{
string commandFileName = Path.GetFileNameWithoutExtension(fi.FullName).ToLower();
switch (commandFileName)
{
case "iexplore":
psi = new ProcessStartInfo(browserCommand, string.Concat("\"", url, "\"", " ", "-extoff -new"));
break;
default:
psi = new ProcessStartInfo(browserCommand, string.Concat("\"", url, "\""));
break;
}
}
psi.UseShellExecute = true; //<- have to set this to make runas work
psi.Verb = "runas"; //<- instructs the process to runas administrator, which will give the user a UAC prompt if UAC is turned on.
Process.Start(psi);
}
}