Want to hide the cmd prompt screen

会有一股神秘感。 提交于 2020-01-14 08:54:22

问题


I have developed a utility which will get time of all servers in the list.

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()

While executing this code. Cmd prompt screens are coming. I want to hide it from the user. What can I do for it?


回答1:


You can tell the process to use no window or to minimize it:

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

// don't show window
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

with UseShellExecute = false you may redirect the output:

// redirect standard output as well as errors
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;

When you do this, you should use asynchronous reading of the output buffers to avoid a deadlock due to overfilled buffers:

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);
                }
            };



回答2:


Try with ProcessWindowStyle enumeration like this;

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;

The hidden window style. A window can be either visible or hidden. The system displays a hidden window by not drawing it. If a window is hidden, it is effectively disabled. A hidden window can process messages from the system or from other windows, but it cannot process input from the user or display output. Frequently, an application may keep a new window hidden while it customizes the window's appearance, and then make the window style Normal. To use ProcessWindowStyle.Hidden, the ProcessStartInfo.UseShellExecute property must be false.




回答3:


Try out this both

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

or check this also

To run the child process without any window,

use the CreateNoWindow property and set UseShellExecute.

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.CreateNoWindow = true; 
info.UseShellExecute = false;
Process processChild = Process.Start(info); 

I suggest you to go throught this post of MSDN : How to start a console app in a new window, the parent's window, or no window




回答4:


Add a System Reference.

using System.Diagnostics;

Then use this code to run your command in a hiden CMD Window.

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.Arguments = "Enter your command here";
cmd.Start();


来源:https://stackoverflow.com/questions/14274360/want-to-hide-the-cmd-prompt-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!