[英]Process.Start doesn't open URL on Windows Server 2008 R2
I am using the following code to successfully open the requested URL in the system default browser on Windows 8.1: 我正在使用以下代码在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();
}
However, on my Windows Server 2008 R2 VM, this same code opens Internet Explorer (the default on that machine), but will only load the URL res://iesetup.dll/SoftAdmin.htm
. 但是,在我的Windows Server 2008 R2 VM上,此相同的代码会打开Internet Explorer(该计算机上的默认设置),但只会加载URL res://iesetup.dll/SoftAdmin.htm
。 IE is set to Enhanced Security Mode OFF. IE设置为“增强安全模式”。 Chrome works as expected on this machine. Chrome可以在这台计算机上正常运行。
Simply invoking Process.Start(url)
also fails to open the requested URL. 仅仅调用Process.Start(url)
也无法打开请求的URL。
When I execute the following from the "Run..." menu, it works as expected: "C:\\Program Files\\Internet Explorer\\iexplore.exe" http://example.com
. 当我从“运行...”菜单执行以下命令时,它将按预期工作: "C:\\Program Files\\Internet Explorer\\iexplore.exe" http://example.com
。 The same goes for start-process http://example.com
in PowerShell. 在PowerShell中, start-process http://example.com
也是如此。
Did you try simply calling Process.Start("http://your_website_here");
您是否尝试过简单地调用Process.Start("http://your_website_here");
? ? It's not like you need to specify browser, if you want to run hyperlinks on default one. 如果要在默认浏览器上运行超链接,则不需要指定浏览器。
@davidsbro - yes, i wouldn't have made it as answer otherwise :) @mark - try http://blog.blksthl.com/2012/11/28/how-to-disable-ie-enhanced-security-in-windows-server-2012/ - that has something to do with server security settings and not application. @davidsbro-是的,否则我不会做出回答:) @mark-尝试http://blog.blksthl.com/2012/11/28/how-to-disable-ie-enhanced-security-in -windows-server-2012 / -与服务器安全设置(而非应用程序)有关。
I wrote a modified version of your above method to start ie with command line options and is a little more detailed in figuring out which process to start. 我编写了上述方法的修改版本以启动,即使用命令行选项启动,并且在确定启动哪个过程时会更加详细。
Also, try changing the Process.Start calls in the code I'm posting to use a specific user and password. 另外,尝试更改我要发布的代码中的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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.