-2

I'm making a console in .Net C# Console and I can't figure out how I would have the user input 2 variables.

I'm making a ping system where the user inputs "ping " and it would ping that ip.

Example

Console.Write($"{Environment.UserName}@root~ $ ");
input = Console.ReadLine();

if (input == $"ping")
{
    goto ping;
}
Employed Russian
  • 199,314
  • 34
  • 295
  • 362

1 Answers1

2

console has several input methode but they act in two ways:

1- when any key is hitted like ReadKey() 2- when Enter key hitted like ReadLine()

so if you want that user input "ping 10.10.10.1" and you app parse that into two distinct strings, you need to do that your self

something like this:

        var pingCmnd = Console.ReadLine().Split(" ");
        
        foreach (var cmnd in pingCmnd)
        {
            if(cmnd.Trim().StartsWith("ping"))
            {
                //do
            }
            else if(!string.IsNullOrEmpty(cmnd.Trim()))
            {
                //do something else with ip
            }
        }
Hadi Bazmi
  • 121
  • 6
  • you can use regex to verify ip, like this: https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp – Hadi Bazmi Dec 25 '21 at 07:07