Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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");
}
like image 772
kamleshrao Avatar asked Jul 30 '16 21:07

kamleshrao


People also ask

What is process start?

Start(ProcessStartInfo) Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

What is process in C#?

C# Process class provides Start method for launching an exe from code. The Process class is in the System. Diagnostics namespace that has methods to run a .exe file to see any document or a webpage. The Process class provides Start methods for launching another application in the C# Programming.


Video Answer


1 Answers

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";
like image 117
dxiv Avatar answered Sep 27 '22 21:09

dxiv