简体   繁体   English

Process.Start(url) 失败

[英]Process.Start(url) fails

I have a WinForms application targeting .NET 2.0.我有一个针对 .NET 2.0 的 WinForms 应用程序。 We have a report that one of our buttons doesn't work, all it does is open a webpage in their default browser.我们有一份报告说我们的一个按钮不起作用,它所做的只是在他们的默认浏览器中打开一个网页。 Looking through the logs I can see Process.Start() fails because it cannot find the file.查看日志我可以看到Process.Start()失败,因为它找不到文件。 The problem is that we pass a string url into the Start() method, so I cannot understand why it generates this message.问题是我们将字符串 url 传递给Start()方法,所以我不明白为什么它会生成此消息。

Here is the exception from the logs:这是日志中的异常:

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)

And for completeness:为了完整性:

Process.Start(url);

Where url has a value of something like: " http://www.example.com "其中 url 的值类似于:“ http://www.example.com

After searching online I came across this blog with the same issue.在网上搜索后,我发现了这个博客也有同样的问题。 The difference is this was specific to Windows 8. He discovered some browsers are not registering themselves correctly when being installed.不同之处在于这是特定于 Windows 8。他发现一些浏览器在安装时没有正确注册自己。 This has since been fixed as the browsers released updates.随着浏览器发布更新,此问题已得到修复。 (Blog dated shortly after Windows 8 release). (博客日期为 Windows 8 发布后不久)。

I could understand it if our customer didn't have a browser installed.如果我们的客户没有安装浏览器,我能理解。 But this is not the case.但这种情况并非如此。 I've also loaded a Windows XP VM, and tried removing all associations for files types of .html , URL: HyperText Transfer Protocol , etc, from the Folder Options window under the File Types tab.我还加载了 Windows XP VM,并尝试从文件类型选项卡下的文件夹选项 window 中删除.htmlURL: HyperText Transfer Protocol等文件类型的所有关联。 But I cannot reproduce the problem.但我无法重现该问题。

Does anyone have any ideas why this might fail, and / or how I can reproduce the error?有谁知道为什么这可能会失败,和/或我如何重现错误?

As a side note, our customer is running Windows XP.作为旁注,我们的客户正在运行 Windows XP。

Had the same problem, solved without IE fallback.有同样的问题,没有IE后备解决。
This will make it behave more like just typing it in the 'Run' window:这将使它的行为更像是在“运行”窗口中键入它:

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

Note that I'm setting UseShellExecute = true请注意,我正在设置UseShellExecute = true

The default is supposed to be true on .Net Framework , and false on .Net Core默认值应该在.Net Framework上为true ,在.Net Core上为false
and UWP apps should not use this flag.并且 UWP 应用不应使用此标志。 see docs 见文档
(I was running on .Net Core) (我在 .Net Core 上运行)

Try using explorer.exe for the fileName explicitly.尝试明确使用explorer.exe作为fileName

As detailed in Process.Start(url) broken on Windows 8/Chrome - are there alternatives?在 Windows 8/Chrome 上损坏的 Process.Start(url) 中详述 - 有替代方案吗?

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

You can open the URL using InternetExplorer which comes along with Windows OS.您可以使用 Windows 操作系统附带的InternetExplorer打开URL

Try This:尝试这个:

Process.Start("IEXPLORE",url);

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

I have this code in a windows forms application and it works fine:我在 Windows 窗体应用程序中有此代码,它工作正常:

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

Specially since your dealing with XP, this is more than likely machine specific issue.特别是因为您使用 XP,这很可能是特定于机器的问题。

I get我明白了

System.ComponentModel.Win32Exception System.ComponentModel.Win32Exception

For WPF Framework project (host=win7, x64).对于 WPF 框架项目 (host=win7, x64)。

Try this:尝试这个:

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

If the browser is not started add Process.Start("chrome.exe", filename) in catch block;如果浏览器未启动,则在 catch 块中添加Process.Start("chrome.exe", filename)

It will start the chrome browser with and tab with "https://www.example.com".它将使用“https://www.example.com”启动 chrome 浏览器和选项卡。

Here the complete example:这里是完整的例子:

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 alternative: .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

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

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

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