How to Read Command Line Arguments

When you program an application for the console, using command line switch arguments allows users to run the software using options. Using C#, the command line characters entered into the console are loaded automatically into the "Main()" function. The programmer can subsequently code the application to run chunks of code that correspond to the options.

Instructions

    • 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!");
      }
      }
      }

Related Searches:

References

Comments

You May Also Like

  • Windows Regedit Command Line Options

    Regedit is a program that allows someone to edit the Windows registry. The registry is a large set of variables that gives...

  • How to Use Command Line Parameters in C++

    Command-line parameters are special words that convey technical information to a program during launch. A user can pass them externally from the...

  • How to Read CSV File in VB

    Comma separated value (CSV) is a simple text file format that is commonly used to store data. A row represents each record...

  • Java Runtime Exec Tutorial

    Runtime offers several ways to call the exec method, which returns a Process object. The simplest way to execute an external application...

  • How to Use FFmpeg With Visual Basic 6

    FFmpeg is a free, open-source application designed to convert video and audio files between different file formats, such the WMV format that...

  • How to Use Switch Case in C#

    Using "If...else" throughout your C# application can be hard to read and may also lead to you making programming mistakes. Instead use...

  • How to Read a Coat of Arms

    Over the centuries, an entire academic discipline has developed around reading coats of arms. Heraldry examines the use of colors, symbols and...

  • How to Use the Draw Command in Visual Basic

    Visual Basic is a wonderful program with many modules. You can use it in combination with Microsoft Office programs or by itself....

  • How to Kill a Process from the Command Line

    When an application is hanging or an unwanted process is running on your PC, it works best to kill the process directly...

Related Ads

Featured