2016-10-10 3 views
0

Ich möchte NETSH-Befehl still (ohne Fenster) ausführen. Ich habe diesen Code geschrieben, aber es funktioniert nicht.Führen Sie einen unbeaufsichtigten Prozess im Hintergrund ohne Fenster

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

Nun, du gibst 'false' für' CreateNoWindow' ein ... also hast du * gebeten * ein Fenster zu erstellen. –

Antwort

0

Fabrikat Benutzer-Shell Ausführung false

proc.StartInfo.UseShellExecute = false; 

und übergeben wahr in showWindow Parameter

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

Dies ist, wie ich es in einem Projekt von mir tun:

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 : "")); 

Es berücksichtigt auch die Ausgaben des Befehls.

+0

vielen Dank. das funktioniert. – user3859999

Verwandte Themen