How to Pass Command Line Arguments using Visual Studio

How to Pass Command Line Arguments using Visual Studio
We wanted to test an application in Visual Studio that accepts command-line parameters for some testing, by specifying different test environments. We review that command-line arguments are passed to the program when they have been invoked from the command line. So here’s what code we’ve tried and work fine:

Visual Studio enables excellent features where you can do this in the,Project Properties window on the Debug tab. Here are the steps to achieve this

1. Right-Click on Project from Solution Explorer Select Properties.

2. In the Project Properties Windows, Navigate to Debug Tab

3. You will Find the text box Command Line

So, here you can type the command line with separated by Space.

paramsvs

Note: Only separate with space ” “.

We write a simple console application to print the command line argument, and put a breakpoint to check the arguments,

paramsvs2

 public class Program
    {
        static void Main(string[] args)
        {

            if (args.Count() != 2)
            {
                Console.WriteLine("Insufficient arguments supplied");
                Console.WriteLine("Service Host must be started as  Executable Name TCP/HTTP and Port");
                Console.WriteLine("Argument 1   Specifying the Binding, which binding will be used to Host (Supported values are either HTTP or TCP) without quotes");
                Console.WriteLine("Argument 2   Port Number a numeric value, the port you want to use");
                Console.WriteLine("\nExamples");
                Console.WriteLine("Executable Name TCP 9001");
                Console.WriteLine("Executable Name TCP 8001");
                Console.WriteLine("Executable Name HTTP 8001");
                Console.WriteLine("Executable Name HTTP 9001");

                return;
            }

            string strBinding = args[0].ToUpper();
            bool bSuccess = ((strBinding == "TCP") || (strBinding == "HTTP"));
            if (bSuccess == false)
            {
                Console.WriteLine("\nBinding argument is invalid, should be either TCP or HTTP)");
                return;
            }
            int nPort = 0;
            bSuccess = int.TryParse(args[1], out nPort);
            if (bSuccess == false)
            {
                Console.WriteLine("\nPort number must be a numeric value");
                return;
            }

 

            bool bindingTCP = (strBinding == "TCP");
            if (bindingTCP) Service.StartTCPService(nPort); else Service.StartHTTPService(nPort);
            if (Service.m_svcHost != null)
            {
                Console.WriteLine("\nPress any key to close the Service");
                Console.ReadKey();
                Service.StopService();
            }
        }
    }
5/5 - (4 votes)