45

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.

DJH
  • 2,191
  • 4
  • 28
  • 45
  • 1
    would you give us the url string you are using? that might help! – Shamim Feb 17 '14 at 17:52
  • Possible duplicate of [Process.Start(url) broken on Windows 8/Chrome - are there alternatives?](http://stackoverflow.com/q/12206368/580951). – Dustin Kingen Feb 17 '14 at 17:57
  • Are you sure that `http://www.example.com` is being passed in on the client's machine and not `www.example.com`? Perhaps you could add logging code in a try-catch block that logged the attemped URL. – Scott Chamberlain Feb 17 '14 at 17:57
  • I didn't put the real value or the url, as it is for our private API. But it is along the lines of `http://website.net/variousparameters`. I can confirm the site is not down, or anything like that. It's one of our users that's reported it, and I am unable to reproduce it. – DJH Feb 17 '14 at 17:59
  • 3
    unfortunately there isn't a lot you can do if the customers machine is corrupted or misconfigured. It's just a fact. The long way round would be to maybe go through the registry to see the default web browser and use that path followed by the command args of the website your and use ProcessStartInfo that way (or Process.Start with the overload accepting the params) – Ahmed ilyas Feb 17 '14 at 18:04
  • @ScottChamberlain Yes, I can confirm the URL is correct. – DJH Feb 17 '14 at 18:12
  • I just tested your string url with Process.Start(url) and it works fine! it just opened my chrome and showed the website! some suggestions: add `www`. add `/` at the end. define your url like this: `string url = @"http://www.website...";` – Shamim Feb 17 '14 at 18:16
  • Have you seen this? http://code.logos.com/blog/2008/01/using_processstart_to_link_to.html – Joel Coehoorn Feb 17 '14 at 18:24

6 Answers6

97

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)

SimpleVar
  • 14,044
  • 4
  • 38
  • 60
19

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);
Community
  • 1
  • 1
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • I just tried installing Firefox and Chrome in XP and setting it as the default. On both occasions I was unable to reproduce the problem. I'll add this change and get the customer to try it. Thanks for your help. – DJH Feb 17 '14 at 18:12
  • Still no luck. I believe [Ahmed ilyay's comment](http://stackoverflow.com/questions/21835891/process-starturl-fails#comment33051080_21835891) is correct that their machine has a problem. Marking your answer as correct as I believe it is the way it should be done. Thanks for all the help, everyone. – DJH Feb 18 '14 at 09:33
  • 1
    If you want to annoy the user by not opening his default browser, you can do this ^^ – Taki7o7 Apr 23 '22 at 08:14
1

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

Try This:

Process.Start("IEXPLORE",url);
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • We left that out so it would launch in the user's [default browser](http://stackoverflow.com/a/10503920/1229237). Also, we cannot guarantee IE will be installed. That's unlikely I admit, but still. Thanks for your help. – DJH Feb 17 '14 at 17:50
  • yes i agree. did you try the same url by manually enetring it in your default browser? – Sudhakar Tillapudi Feb 17 '14 at 17:56
  • The URL is working as expected. It's when it is launched using `Process.Start()` on their machine we have the error. I believe it is something to do with their machine, but no idea what... – DJH Feb 17 '14 at 18:04
  • @DarrenHale: check Romoku answer, it should solve your problem. – Sudhakar Tillapudi Feb 17 '14 at 18:05
1

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

fartwhif
  • 321
  • 3
  • 12
0

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

var info = new ProcessStartInfo(url);
Process.Start(info);
James R.
  • 822
  • 8
  • 17
  • Looking at the stack of the exception, I believe if you use the string overload it defaults to the code you provided. Thanks for your help. – DJH Feb 17 '14 at 18:01
  • Yes, I would think so as well. Was just hoping that perhaps there was a difference in behavior. Of course, now I notice that you say .NET 2.0. This code works great in 4.5. That might be why I don't see any issues. Or perhaps as @Dayan said, maybe it's XP. – James R. Feb 18 '14 at 16:20
-1

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);
}
juagicre
  • 1,065
  • 30
  • 42
PhiseySt
  • 92
  • 10