6

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Karthik Sampath
  • 101
  • 2
  • 7

4 Answers4

12

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);
                }
            };
Matten
  • 17,365
  • 2
  • 42
  • 64
5

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

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

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

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();
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80