DJH
DJH

Reputation: 2459

Process.Start(url) fails

I have a WinForms application targeting .NET 2.0. 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. The problem is that we pass a string url into the Start() method, so I cannot understand why it generates this message.

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"

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. This has since been fixed as the browsers released updates. (Blog dated shortly after Windows 8 release).

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

Upvotes: 60

Views: 35520

Answers (6)

fartwhif
fartwhif

Reputation: 359

Sometimes in Core, even if ProcessInfo.WorkingDirectory is set, Environment.CurrentDirectory also needs to be set.

Upvotes: 1

PhiseySt
PhiseySt

Reputation: 158

I get

System.ComponentModel.Win32Exception

For WPF Framework project (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;

It will start the chrome browser with and tab with "https://www.example.com".

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);
}

Upvotes: -1

YoryeNathan
YoryeNathan

Reputation: 14532

Had the same problem, solved without IE fallback.
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

The default is supposed to be true on .Net Framework, and false on .Net Core
and UWP apps should not use this flag. see docs
(I was running on .Net Core)

Upvotes: 127

Dustin Kingen
Dustin Kingen

Reputation: 21275

Try using explorer.exe for the fileName explicitly.

As detailed in Process.Start(url) broken on Windows 8/Chrome - are there alternatives?

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

Upvotes: 20

James R.
James R.

Reputation: 840

I have this code in a windows forms application and it works fine:

var info = new ProcessStartInfo(url);
Process.Start(info);

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

You can open the URL using InternetExplorer which comes along with Windows OS.

Try This:

Process.Start("IEXPLORE",url);

Upvotes: 1

Related Questions