2016-10-10 35 views
0

我想靜靜地運行NETSH命令(沒有窗口)。 我寫了這段代碼,但它不起作用。在沒有窗口的背景下靜默運行進程

public static bool ExecuteApplication(string Address, string workingDir, string arguments, bool showWindow) 
{ 
    Process proc = new Process(); 
    proc.StartInfo.FileName = Address; 
    proc.StartInfo.WorkingDirectory = workingDir; 
    proc.StartInfo.Arguments = arguments; 
    proc.StartInfo.CreateNoWindow = showWindow; 
    return proc.Start(); 
} 

string cmd= "interface set interface name=\"" + InterfaceName+"\" admin=enable"; 
ExecuteApplication("netsh.exe","",cmd, false); 
+5

那麼你正在''false'傳遞'CreateNoWindow' ...所以你問*它創建一個窗口。 –

回答

0

製作用戶shell執行false

proc.StartInfo.UseShellExecute = false; 

showWindow參數傳遞true

ExecuteApplication("netsh.exe","",cmd, true); 
1

這是我要做的事在我的一個項目:

ProcessStartInfo psi = new ProcessStartInfo();    
psi.FileName = "netsh";    
psi.UseShellExecute = false; 
psi.RedirectStandardError = true; 
psi.RedirectStandardOutput = true; 
psi.Arguments = "SOME_ARGUMENTS"; 

Process proc = Process.Start(psi);     
proc.WaitForExit(); 
string errorOutput = proc.StandardError.ReadToEnd(); 
string standardOutput = proc.StandardOutput.ReadToEnd(); 
if (proc.ExitCode != 0) 
    throw new Exception("netsh exit code: " + proc.ExitCode.ToString() + " " + (!string.IsNullOrEmpty(errorOutput) ? " " + errorOutput : "") + " " + (!string.IsNullOrEmpty(standardOutput) ? " " + standardOutput : "")); 

它也是該命令的輸出。

+0

非常感謝。這樣可行。 – user3859999

相關問題