Process.Start(url) fails
Asked Answered
P

6

55

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.

Pisci answered 17/2, 2014 at 17:46 Comment(8)
would you give us the url string you are using? that might help!Colorist
Possible duplicate of Process.Start(url) broken on Windows 8/Chrome - are there alternatives?.Savanna
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.Enzootic
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.Pisci
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)Attemper
@ScottChamberlain Yes, I can confirm the URL is correct.Pisci
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...";Colorist
Have you seen this? code.logos.com/blog/2008/01/using_processstart_to_link_to.htmlKrasner
S
20

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);
Savanna answered 17/2, 2014 at 18:0 Comment(3)
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.Pisci
Still no luck. I believe Ahmed ilyay's comment 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.Pisci
If you want to annoy the user by not opening his default browser, you can do this ^^Tepefy
S
117

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)

Sham answered 4/4, 2020 at 22:33 Comment(6)
This is the better solution for .NET Core users, not forcing to use any particular browser.Edentate
An update to this. You will need to use "true" if this is WPF on .Net 5. Thank you for such an elegant solution.Swainson
I had to set UseShellExecute = true on .NET 5 for it to work.Cadet
Same on .NET 6, UseShellExecute = true was required.Ternary
Same on .NET 7, UseShellExecute = true is required.Deneb
Same on .NET 8, only worked if I did UseShellExecute = true otherwise it says file not found.Houseyhousey
S
20

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);
Savanna answered 17/2, 2014 at 18:0 Comment(3)
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.Pisci
Still no luck. I believe Ahmed ilyay's comment 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.Pisci
If you want to annoy the user by not opening his default browser, you can do this ^^Tepefy
S
1

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

Try This:

Process.Start("IEXPLORE",url);
Scrophulariaceous answered 17/2, 2014 at 17:47 Comment(4)
We left that out so it would launch in the user's default browser. Also, we cannot guarantee IE will be installed. That's unlikely I admit, but still. Thanks for your help.Pisci
yes i agree. did you try the same url by manually enetring it in your default browser?Scrophulariaceous
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...Pisci
@DarrenHale: check Romoku answer, it should solve your problem.Scrophulariaceous
E
1

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

Exuberate answered 18/9, 2020 at 12:52 Comment(0)
G
0

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

var info = new ProcessStartInfo(url);
Process.Start(info);
Gambill answered 17/2, 2014 at 17:53 Comment(2)
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.Pisci
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.Gambill
C
-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);
}
Contraption answered 11/11, 2020 at 13:56 Comment(2)
If you have a question, consider looking for an answer or posting your own question, never post a question as an answer from another question.Kingsbury
this is my answer, no the question.Contraption

© 2022 - 2024 — McMap. All rights reserved.