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?
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With