这是我面临的一个愚蠢而棘手的问题。
以下代码效果很好(启动计算器):
ProcessStartInfo psStartInfo = new ProcessStartInfo();
psStartInfo.FileName = @"c:\windows\system32\calc.exe";
Process ps = Process.Start(psStartInfo);
然而,SoundRecorder的下面一个不起作用。它给了我"系统找不到指定的文件"错误。
ProcessStartInfo psStartInfo = new ProcessStartInfo();
psStartInfo.FileName = @"c:\windows\system32\soundrecorder.exe";
Process ps = Process.Start(psStartInfo);
我可以使用Start - >启动录音机。运行 - > " C:\ Windows \ System32下\ soundrecorder.exe"命令。
任何想法都会出错?
我在Visual Studio 2015中使用C#并使用Windows 7操作系统。
更新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");
}
答案 0 :(得分:12)
您的应用最有可能是32位,而在64位Windows中,C:\Windows\System32
的引用会被透明地重定向到C:\Windows\SysWOW64
以获取32位应用。 calc.exe
恰好存在于两个地方,而soundrecorder.exe
仅存在于真实的System32
中。
从Start / Run
启动时,父进程是64位explorer.exe
,因此不会进行重定向,并且会找到并启动64位C:\Windows\System32\soundrecorder.exe
。
在大多数情况下,只要32位应用程序尝试访问%windir%\ System32,就会将访问权限重定向到%windir%\ SysWOW64。
[编辑] 来自同一页:
32位应用程序可以通过将%windir%\ Sysnative替换为%windir%\ System32来访问本机系统目录。
因此,以下内容适用于从(真实)soundrecorder.exe
启动C:\Windows\System32
。
psStartInfo.FileName = @"C:\Windows\Sysnative\soundrecorder.exe";
答案 1 :(得分:0)
旧线程,但又提供了一种可能的情况
在我的情况下,我在 Process.Start
中使用了参数7 2015
102 2016
10 1
我将其更改为
System.Diagnostics.Process.Start("C:\\MyAppFolder\\MyApp.exe -silent");
然后它起作用了。
答案 2 :(得分:0)
还有一个案例,类似于 Ranadheer Reddy's answer,但不同到足以让我迷惑一段时间。
我犯了一个简单的错误。我有这个:
ProcessStartInfo info = new ProcessStartInfo("C:\\MyAppFolder\\MyApp.exe ");
info.Arguments = "-silent";
Process.Start(info);
看到应用路径末尾的那个空格了吗?是的。它不喜欢那样。如果包含该文件,它将无法找到您的文件。
解决方案是去除多余的空间。然后它起作用了。
如果您通过启动 "cmd.exe /c MyApp.exe -silent"
将应用程序从启动进程转换为直接运行 "MyApp.exe"
,这是一个很容易犯的错误,这就是我正在做的。我希望在这里记录我的不幸对未来的开发者有所帮助。