C# HOW TO RUN PROGRAM USING COMMAND LINE ARGUMENT [ON HOLD]

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 1472  View(s)
Ratings:
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.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: c# How to run program using Command line argument [on hold]

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!