Link to home
Start Free TrialLog in
Avatar of chadmanvb
chadmanvb

asked on

hide cmd window for a console app

I have a service that calls a .net 2.0 console appliction.  How can I hide this cmd window when it runs?  Currently I see the window popup, it runs, then its gone.
Avatar of EndersDev
EndersDev

Go to Project Properties, select the "Application" tab and locate the "Output type" dropdown button.
     Click that and select "Windows Application" as the type.
Avatar of chadmanvb

ASKER

That does work, but I have the console application write lines to the console.  When I change it to a windows form, it no longer writes the lines.  I need this because this info is captured.  I just want to hide the shell.
Well, if I wanted to capture data and not show a shell I'd write the data out somewhere other than the console window.

But...

//add namespace using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

then

IntPtr hWnd = FindWindow(null, AppName );
if (hWnd != IntPtr.Zero)
{
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
}
I do think you should consider writing the data to a file or something though
Avatar of Mike Tomlinson
Instead of just Process.Start(), create an instance of Process and populate the StartInfo structure so you can set the WindowStyle to Hidden:
Dim ConsoleApp As String = "C:\Users\Mike\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe"

        Dim P As New Process
        P.StartInfo.FileName = ConsoleApp
        P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        P.Start()
        P.WaitForExit()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use this sample code (from MSDN)
Class MyProcess

        Public Shared Sub Main()
            Dim myProcess As New Process()

            Try               

                myProcess.StartInfo.UseShellExecute = False
                ' You can start any process, HelloWorld is an example, that writes some text
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe"
                myProcess.StartInfo.CreateNoWindow = True
                myProcess.StartInfo.RedirectStandardOutput = true;
                myProcess.Start()
                ' This code assumes the process you are starting will terminate itself. 
                ' Given that is is started without a window so you cannot terminate it 
                ' on the desktop, it must terminate itself or you can do it programmatically
                ' from this application using the Kill method.
               Dim myStreamReader As StreamReader = myProcess.StandardOutput
              ' Read the standard output of the spawned process.
              Dim myString As String = myStreamReader.ReadLine()
              Console.WriteLine(myString)

              myProcess.WaitForExit()
              myProcess.Close()
            Catch e As Exception
                Console.WriteLine((e.Message))
            End Try
        End Sub 'Main
    End Class

Open in new window