[英]Process.Start in C# The system cannot find the file specified error
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.但是下面的 SoundRecorder 不起作用。 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.我可以使用 Start -> Run -> "c:\windows\system32\soundrecorder.exe" 命令启动录音机。
Any idea whats going wrong?知道出了什么问题吗?
I am using C# in Visual Studio 2015 and using Windows 7 OS.我在 Visual Studio 2015 中使用 C# 并使用 Windows 7 操作系统。
UPDATE 1 : I tried a File.Exists
check and it shows me MessageBox from the below code:更新 1 :我尝试了
File.Exists
检查,它从以下代码向我显示 MessageBox:
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");
}
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.您的应用程序很可能是 32 位的,并且在 64 位 Windows 中,对
C:\Windows\System32
的引用被透明地重定向到C:\Windows\SysWOW64
以用于 32 位应用程序。 calc.exe
happens to exist in both places, while soundrecorder.exe
exists in the true System32
only. calc.exe
恰好存在于这两个地方,而soundrecorder.exe
仅存在于真正的System32
中。
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.当您从
Start / Run
时,父进程是 64 位explorer.exe
,因此不会进行重定向,并且会找到并启动 64 位C:\Windows\System32\soundrecorder.exe
。
From File System Redirector :从文件系统重定向器:
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64.
在大多数情况下,每当 32 位应用程序尝试访问 %windir%\System32 时,访问都会重定向到 %windir%\SysWOW64。
[ EDIT ] From the same page: [编辑]来自同一页面:
32-bit applications can access the native system directory by substituting %windir%\Sysnative for %windir%\System32.
32 位应用程序可以通过将 %windir%\Sysnative 替换为 %windir%\System32 来访问本机系统目录。
So the following would work to start soundrecorder.exe
from the (real) C:\Windows\System32
.因此,以下内容可以从(真实)
C:\Windows\System32
启动soundrecorder.exe
。
psStartInfo.FileName = @"C:\Windows\Sysnative\soundrecorder.exe";
Old thread but providing one more possible case旧线程,但提供了另一种可能的情况
In my case i was using arguments inside Process.Start就我而言,我在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.然后它起作用了。
One more case, similar to Ranadheer Reddy's answer , but different enough to trip me up for awhile.还有一个案例,类似于Ranadheer Reddy 的回答,但不同之处足以让我绊倒一段时间。
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.如果您通过启动
"cmd.exe /c MyApp.exe -silent"
将应用程序从启动进程转换为直接运行"MyApp.exe"
,这是一个很容易犯的错误,这就是我正在做的。 I hope recording my misfortune here will help future developers.我希望在这里记录我的不幸对未来的开发者有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.