13

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");
}
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
kamleshrao
  • 435
  • 1
  • 4
  • 18

3 Answers3

25

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";
dxiv
  • 16,984
  • 2
  • 27
  • 49
  • Interesting! Whats the workaround here? I want to run the in-built Sound Recorder application from my C# – kamleshrao Jul 30 '16 at 21:40
  • And here is the Microsoft KB article which talks about SYSNATIVE documentation. https://support.microsoft.com/en-us/kb/942589 – kamleshrao Jul 30 '16 at 21:46
  • 2
    It baffles one that, to this day, the "distinction" between 32-bit Windows and 64-bit Windows continues to pose untold engineering challenges to Microsoft. The proper solution is beneath trivial. – Bruce David Wilner Jul 31 '16 at 00:04
  • Another fix seems to be just switching to x64 in configuration manager – Jeremy Thompson Jan 06 '17 at 04:10
13

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.

Ranadheer Reddy
  • 4,202
  • 12
  • 55
  • 77
  • 1
    It helped. Also you can pass arguments in `Start` method: `System.Diagnostics.Process.Start($"{pathToExe}", someArgs);` – Vladislav Jul 26 '20 at 18:25
3

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.

Katie Kilian
  • 6,815
  • 5
  • 41
  • 64