[英]Process.Start() throws an “Access Denied” error
當我執行一個進程並嘗試重定向輸出/錯誤時,我收到以下錯誤:
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)
...
可能有什么不對? 這是一個repro:
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();
}
沒有正當理由讓這個失敗,代碼還沒有達到安全敏感的程度。 這是環境問題,機器上的東西正在干擾。 首先重新啟動,然后禁用反惡意軟件。 如果這沒有幫助,那么使用TaskMgr.exe,進程選項卡並隨意開始查殺進程,幸運的話,你會擊中邪惡的行為者。 在superuser.com上詢問有關讓這台機器再次穩定的問題
您必須確保執行程序的帳戶有權執行您嘗試使用process.start啟動的程序,並且該帳戶有權在系統上創建管道。
你試過刪除redirectOutput嗎? 如果沒有重定向輸出,則不會獲得異常意味着您的用戶無法創建管道,因此您必須將此權限授予用戶。
這應該具有完整的文件路徑和文件名,嘗試啟動文件夾將導致此錯誤。
string path = "C:\\test.exe";
proc.StartInfo.FileName = path;
該應用程序也具有管理權限嗎?
編輯:如果是批處理文件,則需要使用擴展名.bat(例如“batch.bat”)才能正常運行。 此外,如果它是一個批處理文件,它不能為空,否則它將拋出異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.