Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you incorporate "/x" option parameters into a .NET console application?

A lot of command line utilities use parameters, such as:

gacutil /i MyDLL.dll

or

regasm /tlb:MyDll.tlb MyDll.dll

How do I set up a .NET console application to take in command line arguments and, secondarily, how can you emulate handling "option" parameters like /i and /tlb: in the respective examples above?

like image 843
Ben McCormack Avatar asked Dec 09 '22 14:12

Ben McCormack


1 Answers

You declare a parameter for the Main method:

public static void Main(string[] args)

Now you have an array that for your first example contains:

args[0] = "/i"
args[1] = "MyDLL.dll"

You just have to parse the strings to determine what the parameter means. Something along the lines of:

foreach (string cmd in args) {
  if (cmd.StartsWith("/")) {
    switch (cmd.Substring(1)) {
      case "i":
        // handle /i parameter
        break;
      // some more options...
      default:
        // unknown parameter
        break;
    }
  } else {
    // cmd is the filename
  }
}
like image 169
Guffa Avatar answered Mar 15 '23 22:03

Guffa