简体   繁体   English

Process.Start无法在Windows Server 2008 R2上打开URL

[英]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.

相关问题 控制台应用程序的 Process.Start() 无法从 Server 2008 R2 中的 Windows 服务工作 - Process.Start() of a console app not working from Windows Service in Server 2008 R2 DirectX 应用程序无法通过 Process.Start 在 Windows Server 2012 R2 上加载,通过 CMD 或在本地计算机上正常工作 - DirectX app unable to load through Process.Start on Windows Server 2012 R2, works fine through CMD or on local machine Chrome无法使用process.start c#正确打开url - Chrome doesn't open url properly using process.start c# WPF Process.Start(pdf) 不打开 PDF - WPF Process.Start(pdf) Doesn't open the PDF 在Win7和Win8而不是Windows 2012 R2下可运行的Process.Start() - Process.Start() working under Win7 and Win8 but not Windows 2012 R2 为什么Server 2012/2008 R2无法启动threading.task或线程? - why server 2012 / 2008 r2 doesn't start threading.task or thread? Process.Start 打开一个 URL,得到一个异常? - Process.Start to open an URL, getting an Exception? Windows Server 2008 R2-无法创建AAC文件 - Windows server 2008 r2 - can't create AAC files Process.Start()无法正常工作 - Process.Start() doesn't work properly Windows Server 2008 R2 64位中内置的C#.NET应用程序无法在Windows 7 32位中运行 - C# .NET application built in Windows Server 2008 R2 64bit doesn't run in Windows 7 32bit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM