views:

69

answers:

3

I have a .NET WinForms app written in C#. In order to support batch operations, I'd now like to make the app able to run in the console.

Is it possible to have an app that detects on startup whether it is running in the console or not?

What modifications do I need to make in order to achieve this behaviour?

+1  A: 

You could use a main method in program.cs and detect whether command line parameters are passed in, if they are do batch processing, if not show the GUI.

public static void Main(string[] args)
{
 if (args.count > 0) {
  //batch
 } else {
  //gui
 }

}
David
The static main method is already there. You just need to add the conditional processing.
Jim Mischel
yes, sorry i've been doing vb.net too long now.
David
and the downvote was for?
David
Wasn't from me!
Jim Mischel
+2  A: 

You can allocate a Console for your WinForms app using the AllocConsole function. You can find more information about how to call this from C# on it's pinvoke page.

However, this will not make it a true console app and I've heard that there are some limitations, however, it might work depending on your exact needs.

ho1
If you're interested in allocating a console from a Windows Forms application, you'll probably be interested in my ConsoleDotNet wrapper: mischel.com/pubs/consoledotnet/consoledotnet.zip. I wrote this up for DevSource a few years ago. Two of the three articles are still available. Unfortunately, the first article (which explains setting things up) has gone missing.
Jim Mischel
I'm not sure why I'd need to allocate a console: what I want to do is run my app *from* a console.
Tom Wright
@tom: I read your question as you want to interact with a console, so that your app wants to write things back to the console and maybe let the user (or other process) send commands to it via the console. If all you want is to accept some commandline arguments and run without showing a form, then Coding Gorilla's answer is better.
ho1
Though thinking about it, `AttachConsole` would probably have been a more suitable alternative than `AllocConsole` for my answer.
ho1
Thanks ho1 - it's good to know about for future reference!
Tom Wright
@ho1: The problem with `AttachConsole` is that there has to be a console available and you have to know how to locate it. And if somebody closes the console while you're attached to it, your program goes away (unless you do some magic).
Jim Mischel
+6  A: 

You should have a Program.cs file in your solution, this file contains a:

static void Main() 
{
}

You'll notice in this method there is something like:

Application.Run(new Form1());

This is where your form is actually launched, so what you can do is modify your Main() to something like this:

static void Main(string[] args)
{
   if(args.Length < 1)
   {
      Application.Run(new Form1());
      return;
   }
   else
   {
     // Handle your command line arguments and do work
   }
}

So if your program is invoked with no command line arguments, the windows form pops open and does its thing. Otherwise you do what you need to do via the command line and exit without ever showing a form.

Coding Gorilla