Process.Start():系统找不到指定的文件,但我的文件路径似乎是合法的

wha*_*ame 3 c# diagnostics process du

这令人难以置信.使用以下代码:

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();
Run Code Online (Sandbox Code Playgroud)

我跑了,我得到:

未处理的异常:System.ComponentModel.Win32Exception:系统找不到指定的文件

虽然cmdPath的输出值是C:\Windows\system32\du.exe!

当然,如果我只是输入cmdPath命令提示符的内容,它运行du.exe并给我使用信息.

此外,如果我用"du.exe"替换命令路径,并将du.exe放在工作目录中,一切正常.但我想引用系统位置中的那个.

那么发生了什么?据我所知,我有一个合法的文件说明符,但为什么不Process.Start()执行呢?这个基本代码也执行其他几个程序并获得它们的输出.其他都工作正常,虽然du.exe与它们不同,因为它位于system32目录中.这与它有关吗?

谢谢

Dav*_*nan 15

这取决于文件系统重定向器.您将在64位计算机上运行32位进程.这意味着它C:\Windows\system32被透明地重定向到C:\Windows\SysWOW64,我希望du.exe在那里找不到.如果您使用,C:\Windows\Sysnative那么您将能够找到该文件.

但是,我怀疑您已添加du.exe到系统目录,因为这不是标准的Windows组件.你不应该这样做.我建议你把文件放在其他地方,因为你根本就不应该写在系统目录中.