Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to hide the cmd prompt screen

Tags:

c#

.net

command

cmd

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?

like image 284
Karthik Sampath Avatar asked Jan 11 '13 08:01

Karthik Sampath


2 Answers

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);
                }
            };
like image 169
Matten Avatar answered Oct 03 '22 13:10

Matten


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.

like image 26
Soner Gönül Avatar answered Oct 03 '22 13:10

Soner Gönül