Process.Start(): The system cannot find the file specified, but my file path seems to be legit

StackOverflow https://stackoverflow.com/questions/10359204

  •  04-06-2021
  •  | 
  •  

سؤال

This is boggling my mind. Using the following code:

Process du = new Process();          

string cmdPath = System.IO.Path.Combine(Environment.SystemDirectory, "du.exe");
Debug.WriteLine(cmdPath);

ProcessStartInfo info = new ProcessStartInfo(cmdPath);
info.CreateNoWindow = true;

info.Arguments = arguments;            
info.UseShellExecute = false;

info.RedirectStandardOutput = true;
du.StartInfo = info;
du.EnableRaisingEvents = true;
du.OutputDataReceived += responseParser;

du.Start();
du.BeginOutputReadLine();
du.WaitForExit();

I run that, and I get:

Unhandled Exception: System.ComponentModel.Win32Exception: The system cannot find the file specified

though the output value of cmdPath is C:\Windows\system32\du.exe!

and of course, if I just type the contents of cmdPath into a command prompt, it runs du.exe and gives me the usage info.

Also, if I replace the command path with just "du.exe", and put the du.exe in the working directory, all works fine. But I want to reference the one in the system location.

So, what is going on? As far as I can tell, I have a legitimate file specifier, but why won't Process.Start() execute it? This basic code is also executing several other programs and getting their output. The others all work fine, though du.exe is different from them in that it's in the system32 directory. Does that have something to do with it?

Thanks

هل كانت مفيدة؟

المحلول

This is down to the file system redirector. You will be running a 32 bit process on a 64 bit machine. That means that C:\Windows\system32 is transparently redirected to C:\Windows\SysWOW64 and I expect that du.exe cannot be found there. If you use C:\Windows\Sysnative instead then you will be able to locate the file.

However, I suspect that you have added du.exe to the system directory since this is not a standard Windows component. You should not do that. I recommend you put the file somewhere else because you simply should not be writing in the system directory.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top