2011-12-17 101 views
3

當我通過執行過程,並試圖重定向輸出/錯誤,我得到以下錯誤:的Process.Start()拋出一個「拒絕訪問」錯誤

System.ComponentModel.Win32Exception (0x80004005): Access is denied 
at System.Diagnostics.Process.CreatePipe(SafeFileHandle& parentHandle, SafeFileHandle& childHandle, Boolean parentInputs) 
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) 
... 

出了什麼問題?這裏是一個攝製:

string path = "C:\\batch.cmd"; 
using (Process proc = new Process()) 
{ 
    bool pathExists = File.Exists(path); 
    if(!pathExists) throw new ArgumentException("Path doesnt exist"); 

    proc.StartInfo.FileName = path; 
    proc.StartInfo.WorkingDirectory = workingDir.FullName; 
    proc.StartInfo.UseShellExecute = false; 
    proc.StartInfo.RedirectStandardError = true; 
    proc.StartInfo.RedirectStandardOutput = true;  

    proc.Start(); //Exception thrown here 
    proc.WaitForExit(); 
} 
+0

如果您執行'Console.Writeline(File.Exists(path));'?同樣的例外? – tomfanning 2011-12-17 22:45:09

+0

您確定您有權訪問「路徑」中的文件嗎? – 2011-12-17 22:45:20

+0

proc.Start()引發異常。如果我檢查路徑,它存在:bool pathExists = File.Exists(path); \t \t \t \t如果 \t \t \t \t { \t \t \t \t \t拋出新的ArgumentException( 「犯規存在路徑」)(pathExists!); \t \t \t \t} – Gjorgji 2011-12-17 22:49:09

回答

1

這應該有完整的文件路徑和文件名,試圖啓動一個文件夾會導致這個錯誤。

string path = "C:\\test.exe"; 
proc.StartInfo.FileName = path; 

此外,該應用程序是否具有管理權限?

編輯:如果它是一個批處理文件,它需要擴展名爲.bat,如「batch.bat」才能正常運行。另外,如果它是批處理文件,它不能爲空,否則會拋出異常。

+0

以管理員身份運行應用程序。 – Gjorgji 2011-12-17 22:48:10

+0

你可以顯示你傳遞給它的變量值嗎? – John 2011-12-17 22:48:56

2

您必須確保執行程序的帳戶有權執行您嘗試使用process.start啓動的程序,並且該帳戶有權在系統上創建管道。

你試過刪除redirectOutput嗎?如果沒有重定向輸出,您不會得到異常意味着您的用戶無法創建管道,因此您必須將此權限授予用戶。

+0

如果我將RedirectStandardError和RedirectStandardOutput設置爲false,則一切正常。我如何賦予用戶創建管道的權利? – Gjorgji 2011-12-17 22:52:52

+0

嘗試以管理員身份運行該程序並查看會發生什麼情況。 – aleroot 2011-12-17 22:53:33

+0

我有這個完全相同的問題。去了我試圖產生EXE的目錄,並添加了我正在運行的安全選項卡上的讀/執行帳戶,問題就消失了。 – 2015-10-29 13:43:21

3

沒有體面的原因會導致失敗,代碼還沒有到達對安全敏感的地步。這是環境問題,您的計算機上存在干擾。先重新啓動,然後關閉反惡意軟件。如果這沒有幫助,那麼使用TaskMgr.exe,Processes選項卡並隨意啓動殺死進程,運氣好的話你會擊中惡作劇者。詢問關於如何讓本機穩定在superuser.com