这让我很困惑。使用以下代码:
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();
我运行它,我得到:
Unhandled Exception: System.ComponentModel.Win32Exception: The system cannot find the file specified
虽然cmdPath的输出值为C:\Windows\system32\du.exe
!
当然,如果我只是在命令提示符中键入 cmdPath
的内容,它会运行 du.exe 并为我提供使用信息。
此外,如果我只用“du.exe”替换命令路径,并将 du.exe 放在工作目录中,一切正常。但我想引用系统位置中的那个。
那么,这是怎么回事?据我所知,我有一个合法的文件说明符,但为什么 Process.Start()
不执行它?此基本代码还执行其他几个程序并获取它们的输出。其他的都工作正常,虽然 du.exe 与它们的不同之处在于它位于 system32 目录中。跟这个有关系吗?
谢谢
最佳答案
这取决于 file system redirector .您将在 64 位机器上运行 32 位进程。这意味着 C:\Windows\system32
被透明地重定向到 C:\Windows\SysWOW64
并且我预计找不到 du.exe
那里。如果您改用 C:\Windows\Sysnative
,那么您将能够找到该文件。
但是,我怀疑您已将 du.exe
添加到系统目录,因为这不是标准的 Windows 组件。你不应该那样做。我建议您将该文件放在其他地方,因为您根本不应该在系统目录中写入。
关于c# - Process.Start() : The system cannot find the file specified, 但我的文件路径似乎是合法的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10359204/