簡體   English   中英

Process.Start in C# The system cannot find the file specified error

[英]Process.Start in C# The system cannot find the file specified error

這是我面臨的一個愚蠢而棘手的問題。

下面的代碼運行良好(它啟動計算器):

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 -> Run -> "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");
}

您的應用程序很可能是 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 來訪問本機系統目錄。

因此,以下內容可以從(真實) C:\Windows\System32啟動soundrecorder.exe

psStartInfo.FileName = @"C:\Windows\Sysnative\soundrecorder.exe";

舊線程,但提供了另一種可能的情況

就我而言,我在Process.Start中使用了參數

System.Diagnostics.Process.Start("C:\\MyAppFolder\\MyApp.exe -silent");

我把它改成

ProcessStartInfo info = new ProcessStartInfo("C:\\MyAppFolder\\MyApp.exe");
info.Arguments = "-silent";
Process.Start(info)

然后它起作用了。

還有一個案例,類似於Ranadheer Reddy 的回答,但不同之處足以讓我絆倒一段時間。

我犯了一個簡單的錯誤。 我有這個:

ProcessStartInfo info = new ProcessStartInfo("C:\\MyAppFolder\\MyApp.exe ");
info.Arguments = "-silent";
Process.Start(info);

看到應用程序路徑結束后的那個空間嗎? 是的。 它不喜歡那樣。 如果包含該文件,它將找不到您的文件。

解決方案是去除多余的空間。 然后它起作用了。

如果您通過啟動"cmd.exe /c MyApp.exe -silent"將應用程序從啟動進程轉換為直接運行"MyApp.exe" ,這是一個很容易犯的錯誤,這就是我正在做的。 我希望在這里記錄我的不幸對未來的開發者有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM