4

How would I read custom variables end of my application path? I am not sure exactly what they are called, but for example in in the shortcut path of Google Chrome you can add settings to the end of the path such as:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --Custom-setting"

How would I read these arguments after my application has started?

Darren
  • 68,902
  • 24
  • 138
  • 144
jason
  • 1,132
  • 14
  • 32

3 Answers3

4

They are arguments that you are passing to the application. For example take the following application:

static void Main(string[] args)
{
if (args == null)
{
    Console.WriteLine("args is null"); // Check for null array
}
else
{
    Console.Write("args length is ");
    Console.WriteLine(args.Length); // Write array length
    for (int i = 0; i < args.Length; i++) // Loop through array
    {
    string argument = args[i];
    Console.Write("args index ");
    Console.Write(i); // Write index
    Console.Write(" is [");
    Console.Write(argument); // Write string
    Console.WriteLine("]");
    }
}
Console.ReadLine();

If you ran the following from the command line:

"C:\ConsoleApplication1.exe" a b c

The program would display:

args length is 3
args index 0 is [a]
args index 1 is [b]
args index 2 is [c]

http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

http://msdn.microsoft.com/en-us/library/cb20e19t.aspx

Darren
  • 68,902
  • 24
  • 138
  • 144
2

These values will be contained in the args array in the Main() method which serves as the entry point of your application.

static void Main(string[] args) { }
Tim M.
  • 53,671
  • 14
  • 120
  • 163
1

They are called command line arguments. Although you can parse them manually directly from your main() program entry point, there are several helper libraries which can make life even easier for you and your users, such as this one here and here.

StuartLC
  • 104,537
  • 17
  • 209
  • 285