Karthik Sampath
Karthik Sampath

Reputation: 101

Want to hide the cmd prompt screen

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?

Upvotes: 6

Views: 7114

Answers (4)

Roshana Pitigala
Roshana Pitigala

Reputation: 8776

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

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98740

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.

Upvotes: 5

Pranay Rana
Pranay Rana

Reputation: 176886

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

Upvotes: 1

Matten
Matten

Reputation: 17621

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

Upvotes: 12

Related Questions