Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in Process.Start() -- The system cannot find the file specified

Tags:

c#

.net

process

I am using the following code to fire the iexplore process. This is done in a simple console app.

public static void StartIExplorer() {     var info = new ProcessStartInfo("iexplore");     info.UseShellExecute = false;     info.RedirectStandardInput = true;     info.RedirectStandardOutput = true;     info.RedirectStandardError = true;      string password = "password";     SecureString securePassword = new SecureString();      for (int i = 0; i < password.Length; i++)         securePassword.AppendChar(Convert.ToChar(password[i]));      info.UserName = "userName";     info.Password = securePassword;     info.Domain = "domain";      try     {         Process.Start(info);     }     catch (System.ComponentModel.Win32Exception ex)     {         Console.WriteLine(ex.Message);     } } 

The above code is throwing the error The system cannot find the file specified. The same code when run without specifying the user credentials works fine. I am not sure why it is throwing this error.

Can someone please explain?

like image 706
SO User Avatar asked Mar 03 '10 06:03

SO User


People also ask

How do you fix the system Cannot find the file specified error?

Use SFC to fix system cannot finds the file specified error. In Command Prompt, type the following command: “sfc /scannow”. Now press Enter. After scanning and correcting errors, restart the computer and check if the “system cannot find the file specified” error is fixed.

What does the system Cannot find the file specified mean?

As you know, most people encounter "the system cannot find the file specified" error when trying to access or back up some data. This is because the file system of the target device is corrupted or damaged, making your hard drive, USB or external hard drive inaccessible.

Could not load file or assembly system Cannot find file specified?

For the alert error ("The system cannot find the file specified.") Right click on [Solution Program name] then select Build Dependencies> & left click on Build Customizations... then true-checkbox {MASM} then click OK button.


2 Answers

Try to replace your initialization code with:

ProcessStartInfo info      = new ProcessStartInfo(@"C:\Program Files\Internet Explorer\iexplore.exe"); 

Using non full filepath on Process.Start only works if the file is found in System32 folder.

like image 146
Jojo Sardez Avatar answered Sep 25 '22 17:09

Jojo Sardez


You can't use a filename like iexplore by itself because the path to internet explorer isn't listed in the PATH environment variable for the system or user.

However any path entered into the PATH environment variable allows you to use just the file name to execute it.

System32 isn't special in this regard as any directory can be added to the PATH variable. Each path is simply delimited by a semi-colon.

For example I have c:\ffmpeg\bin\ and c:\nmap\bin\ in my path environment variable, so I can do things like new ProcessStartInfo("nmap", "-foo") or new ProcessStartInfo("ffplay", "-bar")

The actual PATH variable looks like this on my machine.

%SystemRoot%\system32;C:\FFPlay\bin;C:\nmap\bin; 

As you can see you can use other system variables, such as %SystemRoot% to build and construct paths in the environment variable.

So - if you add a path like "%PROGRAMFILES%\Internet Explorer;" to your PATH variable you will be able to use ProcessStartInfo("iexplore");

If you don't want to alter your PATH then simply use a system variable such as %PROGRAMFILES% or %SystemRoot% and then expand it when needed in code. i.e.

string path = Environment.ExpandEnvironmentVariables(        @"%PROGRAMFILES%\Internet Explorer\iexplore.exe"); var info = new ProcessStartInfo(path); 
like image 43
Fraser Avatar answered Sep 22 '22 17:09

Fraser