在没有任何窗口的情况下在后台静默运行进程

Dr *_*per 2 c# process netsh

我要静默运行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);
Run Code Online (Sandbox Code Playgroud)

Mih*_*tea 5

这是我在我的项目中这样做的方式:

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 : ""));
Run Code Online (Sandbox Code Playgroud)

它还说明了命令的输出。