Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:55:40+00:00 2026-05-11T01:55:40+00:00

Is there a way to show the console in a Windows application? I want

  • 0

Is there a way to show the console in a Windows application?

I want to do something like this:

static class Program {     [STAThread]     static void Main(string[] args) {         bool consoleMode = Boolean.Parse(args[0]);          if (consoleMode) {             Console.WriteLine('consolemode started');             // ...         } else {             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault(false);             Application.Run(new Form1());         }     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:55:40+00:00Added an answer on May 11, 2026 at 1:55 am

    What you want to do is not possible in a sane way. There was a similar question so look at the answers.

    Then there’s also an insane approach (site down – backup available here.) written by Jeffrey Knight:

    Question: How do I create an application that can run in either GUI (windows) mode or command line / console mode?

    On the surface of it, this would seem easy: you create a Console application, add a windows form to it, and you’re off and running. However, there’s a problem:

    Problem: If you run in GUI mode, you end up with both a window and a pesky console lurking in the background, and you don’t have any way to hide it.

    What people seem to want is a true amphibian application that can run smoothly in either mode.

    If you break it down, there are actually four use cases here:

    User starts application from existing cmd window, and runs in GUI mode User double clicks to start application, and runs in GUI mode User starts application from existing cmd window, and runs in command mode User double clicks to start application, and runs in command mode. 

    I’m posting the code to do this, but with a caveat.

    I actually think this sort of approach will run you into a lot more trouble down the road than it’s worth. For example, you’ll have to have two different UIs’ — one for the GUI and one for the command / shell. You’re going to have to build some strange central logic engine that abstracts from GUI vs. command line, and it’s just going to get weird. If it were me, I’d step back and think about how this will be used in practice, and whether this sort of mode-switching is worth the work. Thus, unless some special case called for it, I wouldn’t use this code myself, because as soon as I run into situations where I need API calls to get something done, I tend to stop and ask myself ‘am I overcomplicating things?’.

    Output type=Windows Application

    using System; using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; using Microsoft.Win32;  namespace WindowsApplication {     static class Program     {         /*     DEMO CODE ONLY: In general, this approach calls for re-thinking      your architecture!     There are 4 possible ways this can run:     1) User starts application from existing cmd window, and runs in GUI mode     2) User double clicks to start application, and runs in GUI mode     3) User starts applicaiton from existing cmd window, and runs in command mode     4) User double clicks to start application, and runs in command mode.      To run in console mode, start a cmd shell and enter:         c:\path\to\Debug\dir\WindowsApplication.exe console         To run in gui mode,  EITHER just double click the exe, OR start it from the cmd prompt with:         c:\path\to\Debug\dir\WindowsApplication.exe (or pass the 'gui' argument).         To start in command mode from a double click, change the default below to 'console'.     In practice, I'm not even sure how the console vs gui mode distinction would be made from a     double click...         string mode = args.Length > 0 ? args[0] : 'console'; //default to console     */          [DllImport('kernel32.dll', SetLastError = true)]         static extern bool AllocConsole();          [DllImport('kernel32.dll', SetLastError = true)]         static extern bool FreeConsole();          [DllImport('kernel32', SetLastError = true)]         static extern bool AttachConsole(int dwProcessId);          [DllImport('user32.dll')]         static extern IntPtr GetForegroundWindow();          [DllImport('user32.dll', SetLastError = true)]         static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);          [STAThread]         static void Main(string[] args)         {             //TODO: better handling of command args, (handle help (--help /?) etc.)             string mode = args.Length > 0 ? args[0] : 'gui'; //default to gui              if (mode == 'gui')             {                 MessageBox.Show('Welcome to GUI mode');                  Application.EnableVisualStyles();                  Application.SetCompatibleTextRenderingDefault(false);                  Application.Run(new Form1());             }             else if (mode == 'console')             {                  //Get a pointer to the forground window.  The idea here is that                 //IF the user is starting our application from an existing console                 //shell, that shell will be the uppermost window.  We'll get it                 //and attach to it                 IntPtr ptr = GetForegroundWindow();                  int  u;                  GetWindowThreadProcessId(ptr, out u);                  Process process = Process.GetProcessById(u);                  if (process.ProcessName == 'cmd' )    //Is the uppermost window a cmd process?                 {                     AttachConsole(process.Id);                      //we have a console to attach to ..                     Console.WriteLine('hello. It looks like you started me from an existing console.');                 }                 else                 {                     //no console AND we're in console mode ... create a new console.                      AllocConsole();                      Console.WriteLine(@'hello. It looks like you double clicked me to start                    AND you want console mode.  Here's a new console.');                     Console.WriteLine('press any key to continue ...');                     Console.ReadLine();                        }                  FreeConsole();             }         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to know if there is a way to show the Application Console
Is there a way to show a progress bar in php? I like to
Im looking for a way to hide the console (in windows) in my program,
Is there a way to show german umlauts like äÄöÖüÜ in the description attribute?
Is there a way to show any error message if IIS(5.1) is stopped or
Is there a way to show the public interface of a package in eclipse?
Is there a way to show different fonts/font-sizes depending on whether it's a Java
is there a way to show the icon for maven site documentation if using
Is there any way to show only week day with Date as: MON 03-JUL
Is there a way to show the SQL that Django is running while performing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.