2017-08-27 109 views
-1

我想通過CMD運行幾個命令(管理員): ipconfig/flushdns ipconfig /registerdns ipconfig /release ipconfig /renew netsh winsock reset 任何人都可以告訴我怎麼做?如何逐個運行一些CMD命令(作爲管理員)

+1

[以編程方式提升進程權限?](https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically) – Vanna

回答

0

這裏排序回答:Running cmd commands with Administrator rights

代碼可以複製(如下所示):

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
    System.Diagnostics.ProcessStartInfo startInfo = new 
    System.Diagnostics.ProcessStartInfo(); 
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
    startInfo.FileName = "cmd.exe"; 
    startInfo.Arguments = "/C ipconfig /flushdns"; 
    startInfo.Verb = "runas"; 
    process.StartInfo = startInfo; 
    process.Start(); 

System.Diagnostics.Process process2 = new System.Diagnostics.Process(); 
    System.Diagnostics.ProcessStartInfo startInfo2 = new 
    System.Diagnostics.ProcessStartInfo(); 
    startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
    startInfo2.FileName = "cmd.exe"; 
    startInfo2.Arguments = "/C ipconfig /renew"; 
    startInfo2.Verb = "runas"; 
    process2.StartInfo = startInfo2; 
    process2.Start(); 
+0

這以管理員身份運行CMD命令,對吧? (即使用戶沒有選擇「以管理員身份運行」) – Leviathan

+0

並且出現以下錯誤:process2.StartInfo2 = startInfo2; – Leviathan

+1

應該是process2.StartInfo = startInfo2 – JSWulf

0

如果您正在尋找使用PowerShell,你可以創建一個腳本,其中將包括你的命令是這樣的:

& "command 1"; & "command 2"; 等問題的

相關問題