繁体   English   中英

Process.Start(url) 失败

[英]Process.Start(url) fails

我有一个针对 .NET 2.0 的 WinForms 应用程序。 我们有一份报告说我们的一个按钮不起作用,它所做的只是在他们的默认浏览器中打开一个网页。 查看日志我可以看到Process.Start()失败,因为它找不到文件。 问题是我们将字符串 url 传递给Start()方法,所以我不明白为什么它会生成此消息。

这是日志中的异常:

System.ComponentModel.Win32Exception: The system cannot find the file specified
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)
   at *namespace*.Website.LaunchWebsiteAsync(String url)
The system cannot find the file specified
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)
   at *namespace*.Website.LaunchWebsiteAsync(String url)

为了完整性:

Process.Start(url);

其中 url 的值类似于:“ http://www.example.com

在网上搜索后,我发现了这个博客也有同样的问题。 不同之处在于这是特定于 Windows 8。他发现一些浏览器在安装时没有正确注册自己。 随着浏览器发布更新,此问题已得到修复。 (博客日期为 Windows 8 发布后不久)。

如果我们的客户没有安装浏览器,我能理解。 但这种情况并非如此。 我还加载了 Windows XP VM,并尝试从文件类型选项卡下的文件夹选项 window 中删除.htmlURL: HyperText Transfer Protocol等文件类型的所有关联。 但我无法重现该问题。

有谁知道为什么这可能会失败,和/或我如何重现错误?

作为旁注,我们的客户正在运行 Windows XP。

有同样的问题,没有IE后备解决。
这将使它的行为更像是在“运行”窗口中键入它:

Process.Start(new ProcessStartInfo("https://www.example.com") { UseShellExecute = true });

请注意,我正在设置UseShellExecute = true

默认值应该在.Net Framework上为true ,在.Net Core上为false
并且 UWP 应用不应使用此标志。 见文档
(我在 .Net Core 上运行)

尝试明确使用explorer.exe作为fileName

在 Windows 8/Chrome 上损坏的 Process.Start(url) 中详述 - 有替代方案吗?

Process.Start("explorer.exe", url);

您可以使用 Windows 操作系统附带的InternetExplorer打开URL

尝试这个:

Process.Start("IEXPLORE",url);

对于Core中的一些用例,即使设置了ProcessInfo.WorkingDirectory ,也需要设置Environment.CurrentDirectory :API的一个不直观的差异无疑会导致大量的移植代码中断和混乱。

我在 Windows 窗体应用程序中有此代码,它工作正常:

var info = new ProcessStartInfo(url);
Process.Start(info);
try
{
  Process.Start(url);
}
catch (Win32Exception)
{
  Process.Start("IExplore.exe", url);
}

特别是因为您使用 XP,这很可能是特定于机器的问题。

我明白了

System.ComponentModel.Win32Exception

对于 WPF 框架项目 (host=win7, x64)。

尝试这个:

filename="https://www.example.com"; 
Process.Start(filename) 

如果浏览器未启动,则在 catch 块中添加Process.Start("chrome.exe", filename)

它将使用“https://www.example.com”启动 chrome 浏览器和选项卡。

这里是完整的例子:

try
{
    var runningProcess = Process.GetProcessesByName("chrome");
    if (runningProcess.Length != 0)
    {
        Process.Start("chrome", filename);
        return;
    }
    runningProcess = Process.GetProcessesByName("firefox");
    if (runningProcess.Length != 0)
    {
        Process.Start("firefox", filename);
        return;
    }
    runningProcess = Process.GetProcessesByName("iexplore");
    if (runningProcess.Length != 0)
    {
        Process.Start("iexplore", filename);
        return;
    }
    Process.Start(filename);
}
catch (System.ComponentModel.Win32Exception)
{
    Process.Start("chrome.exe", filename);
}

.NET Core 6、VB.NET 替代方案:

    Public Sub URLOpen(Url As String)

       Dim OpenURL As New ProcessStartInfo With {
        .UseShellExecute = True,
        .FileName = "explorer.exe",
        .Arguments = Url
       }

       Process.Start(OpenURL)

    End Sub

所以,你可以URLOpen("https://www.example.com/")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM