简体   繁体   中英

c# Command Line arguments

I am trying to make my program run from commands line arguments, so i have 3 options in my code that you can choose to run.

The question is that i want to parse the port along with the arguments, how do i do that?

Each option has a different program configuration. My attempt show below; so inside the the program i also want to pass the the port as a argument so when i write "program 1 5656" in the console. The application sees that its the first option 1 to run and then parses the 5656 into the port variable.

I tried below but when i enter the command it gives me the wrong option as in it start option 2 instead of 1.

class MainClass
{
    static void Main(string[] args)
    {
        // Test if input arguments were supplied:
        if (args.Length == 1)
        {
            int port = int.Parse(args[1]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
        if (args.Length == 2)
        {
            int port = int.Parse(args[2]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
        if (args.Length == 3)
        {
            int port = int.Parse(args[3]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
    }
}

It seems, that you want port being the last parameter:

  static void Main(string[] args) {  
    // if we have parameters...
    if (args.Length > 0) { 
      //TODO: int.TryParse is a better choice
      int port = int.Parse(args[args.Length - 1]); // ... port is the last one
      server = new TcpListener(IPAddress.Any, port);
      // Rest of the program
    }
  }

Edit : if you want to pass just two parameters ( option and port )

  static void Main(string[] args) { 
    if (args.Length == 2) {
      //TODO: int.TryParse is a better choice 
      int option = int.Parse(args[0]);
      int port = int.Parse(args[1]);

      // Rest of the program, e.g.
      if (option == 1) {
        ...  
      } 
      else if (option == 2) {
        ...
      }
      else if (option == 3) {
        ...
      }   
    }
  }

Is this, what you want?

class MainClass
{
    static void Main(string[] args)
    {
        // Test if input arguments were supplied:
        var switchvalue = int.Parse(args[0]);
        if (switchvalue == 1)
                {
            int port = int.Parse(args[1]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
        if (switchvalue == 2)
        {
            int port = int.Parse(args[1]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
        if (switchvalue == 3)
        {
            int port = int.Parse(args[1]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
    }
}

在此处输入图片说明

public Main (string[] args)
{
   Console.WriteLine(args);
   Console.ReadLine();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM