Home > DeveloperSection > Forums > c# How to run program using Command line argument [on hold]
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    September-04-2013 2:29 AM

 C# C# 
Ratings:


 1 Reply(s)
 1123  View(s)
Rate this:

How to run a program using command line argument for example my program only runs when i say a string e.g Hello World if this value does not match then it should give an error.

if (args == null)

{

    Console.WriteLine("cant be null"); // Check for null array

}

if (args[0].Contains("Hello World"))

{

    Console.WriteLine("True"); // Check for null array

}

else

{

    Console.WriteLine("Error"); // Check for null array

}

Thanks in advance.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-04-2013 7:05 AM

Hi Ankita,

Write this code in your main method(I am considering you are doing this in a Console application);

        if (args.Length <= 0)

        {

            //Error

        }

        else if (args.Length >= 1)

        {

            if (args[0] == @"Your argument")

            {

                //No error.

            }

            else

            {

                //Error

            }

        }

Hope this helps you.


Don't want to miss updates? Please click the below button!

Follow MindStick