Get command line arguments

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet obtains a command line argument and converts it to a number in the Main method.

public static void Main (string[] args)
{
   if (args.Length > 0)
   {
      System.Console.WriteLine (args[0]);
      try
      {
//       long number = Int64.Parse(args[0]);
//       long number = Convert.ToInt64(s);
         long number = long.Parse (args[0]);
         System.Console.WriteLine ("Number = " + number);
      }
      catch (System.FormatException e)
      {
         System.Console.WriteLine ("FormatException" + e);
      }
      return;
   }
   System.Console.WriteLine ("Please enter a number");
}

Visual C# Best Practices

Always put format conversions inside try-catch blocks.


Personal tools