-3

I have created a C# command line application that creates a few reports automatically for my client. However, even though they use 95% of the same code I would like to split them in to different processes. I am using Windows task scheduler to run them. How do I set up the C# application to accept Command line parameters upon runtime?

I cannot find any explanation of this on the internet.

djblois
  • 881
  • 16
  • 45
  • 7
    "I cannot find any explanation of this on the internet. " -- then you must not have tried hard enough. This is basic basic stuff. Very well documented. Check MSDN. – rory.ap Mar 01 '17 at 15:34
  • 2
    Other than `static void Main(string[] args)`? – stuartd Mar 01 '17 at 15:35

2 Answers2

1

All command line parameters are passed to your application through string[] args parameter.
The code below shows an example:

using System.Linq;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Contains("/REPORT1"))
            {
                /* Do something */
            }
            else if (args.Contains("/REPORT2"))
            {
                /* Do something */
            }
        }
    }
}

Then, in command prompt just use:

C:\MyApp\MyApp.exe /REPORT1
0

MSDN

The below snippet will print the number of parameters passed from the command line... string[] args contains the parameter values...

class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}
Kyle
  • 6,250
  • 2
  • 30
  • 39
Shane Ray
  • 1,379
  • 1
  • 12
  • 18