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?
You should have a Program.cs file in your solution, this file contains a:
You’ll notice in this method there is something like:
This is where your form is actually launched, so what you can do is modify your
Main()to something like this: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.