'How to get command line parameters and put them into variables? [duplicate]

I am trying to make an application. Can someone help me how to get command line parameters and put them into variables/strings. I need to do this on C#, and it must be 5 parameters.

The first parameter needs to be put into Title variable. The second parameter needs to be put into Line1 variable. The third parameter needs to be put into Line2 variable. The fourth parameter needs to be put into Line3 variable. And the fifth parameter needs to be put into Line4 variable.

Tank You For Helping!

Edit:

I need to add this into Windows Forms Application.



Solution 1:[1]

Consider using a library to do all this parsing for you. I have had success using the Command Line Parser library on Github:

https://github.com/commandlineparser/commandline

You can get this library using Nuget:

Install-Package CommandLineParser

And here is a sample usage:

// Define a class to receive parsed values
class Options
{
    [Option('r', "read", Required = true, HelpText = "Input file to be processed.")]
    public string InputFile { get; set; }

    [Option('v', "verbose", DefaultValue = true, HelpText = "Prints all messages to standard output.")]
    public bool Verbose { get; set; }

    [ParserState]
    public IParserState LastParserState { get; set; }

    [HelpOption]
    public string GetUsage() {
    return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
    }
}

// Consume them
static void Main(string[] args)
{
    var options = new Options();

    if (CommandLine.Parser.Default.ParseArguments(args, options))
    {
        // Values are available here
        if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
    }
}

Solution 2:[2]

The command line args are found in the args array'

public static void Main(string[] args)
   {
       // The Length property is used to obtain the length of the array. 
       // Notice that Length is a read-only property:
       Console.WriteLine("Number of command line parameters = {0}",
          args.Length);
       for(int i = 0; i < args.Length; i++)
       {
           Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
       }
   }

Source http://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 TGH