Go to GoReading for breaking news, videos, and the latest top stories in world news, business, politics, health and pop culture.

How to Read Command Line Arguments

104 20
    • 1). Create a new console application in Visual Studio by clicking the "File" menu and selecting "New." Select "Windows Console Application" from the list and click "OK."

    • 2). Evaluate the main function. When the project is created, the main function is the first thing compiled and run. Within this function, the command line arguments are passed. The following is an example of the main function. Arguments typed in the command prompt are saved as an array in the "args" parameter.
      static int Main(string[] args)
      {
      }

    • 3). Detect if there are arguments entered in the prompt. The following code checks if the user typed in any artuments.
      static int Main(string[] args)
      {
      if (args.Length != 0) //there is at least 1 argument
      {
      }
      }

    • 4). Print the first command line argument to the console. If an argument exists, the following code displays it to the user.
      static int Main(string[] args)
      {
      if (args.Length != 0) //there is at least 1 argument
      {
      if (arg[0] == "Hello!")
      {
      System.Console.WriteLine("User typed in Hello!");
      }
      }
      }

Source...

Leave A Reply

Your email address will not be published.