想要隐藏cmd提示屏幕

Kar*_*ath 6 .net c# command cmd

我开发了一个实用程序,它将获得列表中所有服务器的时间.

System.Diagnostics.Process p;
string server_name = "";
string[] output;
p = new System.Diagnostics.Process();
p.StartInfo.FileName = "net";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StandardOutput.ReadLine().ToString()
Run Code Online (Sandbox Code Playgroud)

执行此代码时.Cmd提示屏幕即将到来.我想将其隐藏起来.我能做些什么?

Mat*_*ten 12

您可以告诉进程不使用窗口或将其最小化:

// don't execute on shell
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;

// don't show window
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
Run Code Online (Sandbox Code Playgroud)

UseShellExecute = false你可以重定向输出:

// redirect standard output as well as errors
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
Run Code Online (Sandbox Code Playgroud)

执行此操作时,应使用输出缓冲区的异步读取以避免由于过量填充缓冲区导致的死锁:

StringBuilder outputString = new StringBuilder();
StringBuilder errorString = new StringBuilder();

p.OutputDataReceived += (sender, e) =>
            {
                if (e.Data != null)
                {
                    outputString.AppendLine("Info " + e.Data);
                }
            };

p.ErrorDataReceived += (sender, e) =>
            {
                if (e.Data != null)
                {
                    errorString.AppendLine("EEEE " + e.Data);
                }
            };
Run Code Online (Sandbox Code Playgroud)


Son*_*nül 5

尝试使用这样的ProcessWindowStyle枚举;

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
Run Code Online (Sandbox Code Playgroud)

隐藏的窗口样式.窗口可以是可见的或隐藏的.系统通过不绘制来显示隐藏的窗口.如果窗口被隐藏,则会被有效禁用.隐藏窗口可以处理来自系统或其他窗口的消息,但它无法处理来自用户或显示输出的输入.通常,应用程序可以在自定义窗口外观时隐藏新窗口,然后使窗口样式为"正常".要使用 ProcessWindowStyle.Hidden,ProcessStartInfo.UseShellExecute 属性必须为false.