Disable a command window from a running command-line application using .NET.

I have a module that should run a small .Net command line program to check for updates. Everything works fine, but I am unable to suppress the command line output.

The application has its own form of Windows, which it pops up if it detects an update. The update should be performed as a separate application due to the fact that it requires a different execution context from the DLL from which it is launched.

string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + AUTO_UPDATE_EXENAME; updater.StartInfo.FileName = path; updater.StartInfo.Arguments = AUTO_UPDATE_PARAMETERS; updater.StartInfo.CreateNoWindow = false; updater.StartInfo.UseShellExecute = false; updater.StartInfo.RedirectStandardOutput = true; updater.StartInfo.WorkingDirectory = path; updater.Start(); 

I tried most of all working combinations of CreateNoWindow , UseShellExecute and RedirectStandardOutput , and each of them leads to an annoying black box. The application writes to stdout, but I use it only for debugging, and the user should not really see the text that it generates.

Presumably CreateNoWindow and / or RedirectStandardOutput should prevent the window from appearing, but it doesn't matter how I set these variables.

+4
source share
3 answers

Install the command-line application in the Winforms application, but do not open the form when it runs, as usual.

+5
source

You can hide the window at startup as follows:

 using System.Runtime.InteropServices; namespace MyConsoleApp { class Program { [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [STAThread()] static void Main(string[] args) { Console.Title = "MyConsoleApp"; if (args.StartWith("-w")) { // hide the console window setConsoleWindowVisibility(false, Console.Title); // open your form Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run( new frmMain() ); } // else don't do anything as the console window opens by default } public static void setConsoleWindowVisibility(bool visible, string title) { //Sometimes System.Windows.Forms.Application.ExecutablePath works // for the caption depending on the system you are running under. IntPtr hWnd = FindWindow(null, title); if (hWnd != IntPtr.Zero) { if (!visible) //Hide the window ShowWindow(hWnd, 0); // 0 = SW_HIDE else //Show window again ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA } } } } 

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd

+3
source

Here is an example of the code that the MAC requests in an active connection, this is a console application, you do not need to do this with a Windows form ...

  public class TestARP
     {
         private StringBuilder sbRedirectedOutput = new StringBuilder ();
         public string OutputData
         {
             get {return this.sbRedirectedOutput.ToString ();  }
         }

         // Asynchronous!
         public void Run ()
         {
             System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo ();
             ps.FileName = "arp";
             ps.ErrorDialog = false;
             ps.Arguments = "-a";
             ps.CreateNoWindow = true;  // comment this out
             ps.UseShellExecute = false;  // true
             ps.RedirectStandardOutput = true;  // false
             ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;  // comment this out

             using (System.Diagnostics.Process proc = new System.Diagnostics.Process ())
             {
                 proc.StartInfo = ps;
                 proc.Exited + = new EventHandler (proc_Exited);
                 proc.OutputDataReceived + = new System.Diagnostics.DataReceivedEventHandler (proc_OutputDataReceived);
                 proc.Start ();
                 proc.WaitForExit ();
                 proc.BeginOutputReadLine ();  // Comment this out
             }
         }

         void proc_Exited (object sender, EventArgs e)
         {
             System.Diagnostics.Debug.WriteLine ("proc_Exited: Process Ended");
         }

         void proc_OutputDataReceived (object sender, System.Diagnostics.DataReceivedEventArgs e)
         {
             if (e.Data! = null) this.sbRedirectedOutput.Append (e.Data + Environment.NewLine);
         }
     }

Now consider the Run method, which is in asynchronous mode, and runs as a single console window - in fact, this is a normal console application without a pop-up window, pay attention to comments, if you had to change these lines, it becomes a synchronous process, cost very fast , you will notice that this console will create another window with the output of the arp command. Since it is in asynchronous mode, the output is redirected to the event handler, which fills the data in the StringBuilder instance for further processing ...

Hope this helps, Regards, Tom.

0
source

Source: https://habr.com/ru/post/1300489/


All Articles