6

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.

Mihail Mojsoski
  • 79
  • 1
  • 1
  • 5
  • 1
    Uhhh... use the `args` array in main and use variable assignment? You aren't seriously asking how to do variable assignment are you? I also removed the tags from your title, please they should just be tags. – BradleyDotNET Jul 09 '14 at 18:17
  • Being a noob is fine, not doing any research; not so much. MSDN and SO both have tons of information on this. – BradleyDotNET Jul 09 '14 at 18:22
  • Winforms doesn't *really* change the question, just makes it a bigger pain. The only way to do it is to read the args off of main (yes, it is still there) and pass it up to your form via the constructor or some other mechanism. How you do that piece is up to you of course. – BradleyDotNET Jul 09 '14 at 18:25

3 Answers3

16

You can do it in one of two ways.

The first way is to use string[] args and pass that from Main to your Form, like so:

// Program.cs
namespace MyNamespace
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyForm(args));
        }
    }
}

And then in MyForm.cs do the following:

// MyForm.cs
namespace MyNamespace
{
    public partial class MyForm : Form
    {
        string Title, Line1, Line2, Line3, Line4;
        public MyForm(string[] args)
        {
            if (args.Length == 5)
            {
                Title = args[0];
                Line1 = args[1];
                Line2 = args[2];
                Line3 = args[3];
                Line4 = args[4];
            }
        }
    }
}

The other way is to use Environment.GetCommandLineArgs(), like so:

// MyForm.cs
namespace MyNamespace
{
    public partial class MyForm : Form
    {
        string Title, Line1, Line2, Line3, Line4;
        public MyForm()
        {
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length == 6)
            {
                // note that args[0] is the path of the executable
                Title = args[1];
                Line1 = args[2];
                Line2 = args[3];
                Line3 = args[4];
                Line4 = args[5];
            }
        }
    }
}

and you would just leave Program.cs how it was originally, without the string[] args.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
  • @Mihail Assuming you used my first solution, in `Program.cs` you need to have the following line: `Application.Run(new MyForm(args));`. Note that you must have `args` in there. – Jashaszun Jul 09 '14 at 18:38
7

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);
    }
}
Eric Scherrer
  • 3,328
  • 1
  • 19
  • 34
4

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

TGH
  • 38,769
  • 12
  • 102
  • 135