简体   繁体   中英

Process.Start in C# The system cannot find the file specified error

This is a silly and tricky issue that I am facing.

The below code works well (it launches Calculator):

ProcessStartInfo psStartInfo = new ProcessStartInfo();
psStartInfo.FileName = @"c:\windows\system32\calc.exe";

Process ps = Process.Start(psStartInfo);

However the below one for SoundRecorder does not work. It gives me "The system cannot find the file specified" error.

ProcessStartInfo psStartInfo = new ProcessStartInfo();
psStartInfo.FileName = @"c:\windows\system32\soundrecorder.exe";

Process ps = Process.Start(psStartInfo);

I am able to launch Sound Recorder by using Start -> Run -> "c:\windows\system32\soundrecorder.exe" command.

Any idea whats going wrong?

I am using C# in Visual Studio 2015 and using Windows 7 OS.

UPDATE 1 : I tried a File.Exists check and it shows me MessageBox from the below code:

if (File.Exists(@"c:\windows\system32\soundrecorder.exe"))
{
    ProcessStartInfo psStartInfo = new ProcessStartInfo();
    psStartInfo.FileName = @"c:\windows\system32\soundrecorder.exe";

    Process ps = Process.Start(psStartInfo);
}
else
{
    MessageBox.Show("File not found");
}

Most likely your app is 32-bit, and in 64-bit Windows references to C:\Windows\System32 get transparently redirected to C:\Windows\SysWOW64 for 32-bit apps. calc.exe happens to exist in both places, while soundrecorder.exe exists in the true System32 only.

When you launch from Start / Run the parent process is the 64-bit explorer.exe so no redirection is done, and the 64-bit C:\Windows\System32\soundrecorder.exe is found and started.

From File System Redirector :

In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64.


[ EDIT ] From the same page:

32-bit applications can access the native system directory by substituting %windir%\Sysnative for %windir%\System32.

So the following would work to start soundrecorder.exe from the (real) C:\Windows\System32 .

psStartInfo.FileName = @"C:\Windows\Sysnative\soundrecorder.exe";

Old thread but providing one more possible case

In my case i was using arguments inside Process.Start

System.Diagnostics.Process.Start("C:\\MyAppFolder\\MyApp.exe -silent");

I changed it to

ProcessStartInfo info = new ProcessStartInfo("C:\\MyAppFolder\\MyApp.exe");
info.Arguments = "-silent";
Process.Start(info)

Then it worked.

One more case, similar to Ranadheer Reddy's answer , but different enough to trip me up for awhile.

I was making a simple mistake. I had this:

ProcessStartInfo info = new ProcessStartInfo("C:\\MyAppFolder\\MyApp.exe ");
info.Arguments = "-silent";
Process.Start(info);

See that space after the end of the path to the app? Yeah. It doesn't like that. It will not find your file if you include that.

The solution was to remove the extraneous space. Then it worked.

This is an easy enough mistake to make if you're converting an app from starting processes by launching "cmd.exe /c MyApp.exe -silent" to running "MyApp.exe" directly instead, which is what I was doing. I hope recording my misfortune here will help future developers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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