【问题标题】:Process.Start doesn't open URL on Windows Server 2008 R2Process.Start 无法在 Windows Server 2008 R2 上打开 URL
【发布时间】:2014-10-22 12:37:32
【问题描述】:

我正在使用以下代码在 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) 也无法打开请求的 URL。

当我从“运行...”菜单执行以下操作时,它按预期工作:"C:\Program Files\Internet Explorer\iexplore.exe" http://example.com。 PowerShell 中的 start-process http://example.com 也是如此。

【问题讨论】:

  • 您可以在管理员模式下运行您的应用程序吗?可能与权限有关
  • 好主意,但我以管理员身份运行。
  • 通过更改一些注册表来禁用 IE 安全模式警告,social.technet.microsoft.com/Forums/windowsserver/en-US/…
  • @Ryios 这些建议的注册表更改无效。
  • 您是否阅读过他谈到将其更改为 64 位和 32 位版本的帖子?进行更改后您是否注销并重新登录?

标签: c# internet-explorer windows-server-2008 process.start


【解决方案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/ - 这与服务器安全设置有关,而不是应用程序。

【讨论】:

  • 这应该是一个评论,因为它在问一个问题;但是,我知道您对此没有足够的代表,否则我会对此投反对票。
  • 或许可以改写为答案
  • @Vytautas 是的,这是我尝试的第一件事。 Process.Start(url)和直接打开IE到res://iesetup.dll/SoftAdmin.htm效果一样。
【解决方案2】:

我为您的上述方法编写了一个修改版本,即使用命令行选项启动,并且更详细地确定了启动哪个进程。

另外,请尝试更改我发布的代码中的 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);
        }
    }

【讨论】:

  • 该代码在我的 WPF 应用程序中将我带到此 URL:about:NoAdd-ons。从命令行作为独立的可执行文件,它可以正常工作。
  • 它基本上忽略了 %1 参数,并打开 Internet 选项 -> 常规 -> 主页中设置的任何内容
  • 它应该将 url 作为参数传递,我没有测试非 iexplorer 执行可能是一个错误。我会检查的。
  • 是的,我在 windows server 2008 r2 上的控制台应用程序中运行了它,它对我来说很好,打开我告诉它的 url,默认为 Iexplore.exe,或者 chrome,或 firefox。
  • 同样,它作为控制台应用程序运行良好,但我的 WPF 应用程序中的相同代码是行为不端的原因。
猜你喜欢
  • 2011-06-15
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
相关资源
最近更新 更多