2014-09-01 577 views
4

我試圖運行從另一個用戶帳戶名稱的exe文件,它顯示了以下錯誤請求的操作需要提升

System.ComponentModel.Win32Exception: The requested operation requires an elevation 
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() 
    at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) 

這裏是我的代碼

ProcessStartInfo pro = new ProcessStartInfo(application); 
pro.UseShellExecute = false; 
pro.Verb = "runas"; 
pro.WorkingDirectory = workingdirectory; 
pro.RedirectStandardInput = true; 
pro.RedirectStandardOutput = true; 
pro.CreateNoWindow = true; 

Process process = Process.Start(pro); 

如何解決這個問題?

+0

我最初的問題是你是否試圖以管理員身份運行它(你的應用程序)? – 2014-09-01 07:08:12

+0

是的..我以管理員身份啓動它 – user3797438 2014-09-01 07:08:53

+1

我記得在某處閱讀'runas'需要'UseShellExecute = true'。如果你將'UseShellExecute'設置爲'true',它會工作嗎? – Heinzi 2014-09-01 07:12:27

回答

10

不幸的是,你不能做

  • 提升的權限運行和
  • 重定向輸入/輸出

同時進行。

原因:

  • VerbUseShellExecute = true,但
  • 重定向IO需要UseShellExecute = false只認可。

的更多信息:

我在你的處境猜你將不得不使用runas跳過,而是確保應用程序已經開始與正確的用戶帳戶/權限。這應該起作用,因爲processes started by elevated processes "inherit" elevation

+0

我只能使用提升權限運行....我不想重定向輸入/output...can你建議我的代碼.. @ Heinzi – user3797438 2014-09-01 07:25:43

+0

有一種方法可以在清單中寫一個應用程序應該始終以管理員身份運行,對吧?我不記得是如何因爲它已經有一段時間了,但是'd可能是一件好事,我確信有地方記錄它 – 2014-09-01 07:26:52

+0

@ user3797438:如果你不想重定向輸入/輸出,爲什麼你要將'RedirectStandardInput'設置爲'true'?當然,你的代碼中的每一行都有一個很好的理由,對嗎? – Heinzi 2014-09-01 07:45:44

相關問題