Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress command window from started command-line application with .NET

I have a module that needs to run a small .Net command-line program to check for updates. Everything is working great, however I am having trouble suppressing the Command Prompt output from being shown.

The app has it's own Windows Form that it pops up if it detected an update. Updating needs to run as a seperate app due to the fact that it requires a different execution context from the DLL it is launched from.

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 have tried most all of the different working combinations of CreateNoWindow, UseShellExecute, and RedirectStandardOutput and each of them results in that annoying black box popping up. The app does write to stdout but I only use that for debugging and the user shouldn't really see the text that it generates.

Supposedly CreateNoWindow and/or RedirectStandardOutput should prevent the box from popping up, but it does no matter how I set these variables.

like image 821
Tom Corelis Avatar asked Feb 28 '23 15:02

Tom Corelis


1 Answers

You can hide the window on startup like this:

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

like image 55
Robert Harvey Avatar answered Apr 28 '23 22:04

Robert Harvey