ktconrad90
ktconrad90

Reputation: 187

C# console application executing code when Console.ReadLine() executes

I'm currently messing around with a Console application and I have some logic to get user input for 3 different things. I have the application designed so that the user can type 'Q' or 'q' at any time to exit the program. However, the way I am currently accomplishing this is through if statements after each user input (using the Console.ReadLine().)

A solution I thought of that would be better is to have a piece of code in one place that exits the program and is called automatically when the ReadLine() is executed and checks the input to see if it is 'q' or 'Q'. I was curious if there was any way to do something like this???

Here is the code I have now

Console.WriteLine("Please give me a source and destination directory...(Enter 'Q' anytime to exit)");

        Console.Write("Enter source path: ");
        _sourcePath = Console.ReadLine();
        if (_sourcePath.Equals("q", StringComparison.CurrentCultureIgnoreCase))
        {
            Environment.Exit(Environment.ExitCode);
        }

        Console.Write("Enter destination path: ");
        _destinationPath = Console.ReadLine();
        if (_destinationPath.Equals("q", StringComparison.CurrentCultureIgnoreCase))
        {
            Environment.Exit(Environment.ExitCode);
        }

        Console.Write("Do you want detailed information displayed during the copy process? ");
        string response = Console.ReadLine();
        if (response.Equals("q", StringComparison.CurrentCultureIgnoreCase))
        {
            Environment.Exit(Environment.ExitCode);
        }

        if (response?.Substring(0, 1).ToUpper() == "Y")
        {
            _detailedReport = true;
        }

It would be nice to remove the if blocks and just have the incoming value from the Console.ReadLine() checked when it is executed...

Upvotes: 1

Views: 856

Answers (2)

Daniel Masterson
Daniel Masterson

Reputation: 534

I'm afraid there's no direct way to hook into the ReadLine() call. Wrapping it all in your own method called 'ReadLine' could work though, say something like

static string ReadLine()
{
    string line = Console.ReadLine();

    if (line.Equals("q", StringComparison.CurrentCultureIgnoreCase))
    {
        Environment.Exit(Environment.ExitCode);
    }
    //Other global stuff

    return line;
}

//Elsewhere
Console.Write("Enter source path: ");
_sourcePath = ReadLine(); //Note: No 'Console.' beforehand. This is your method!
Console.Write("Enter destination path: ");
_destinationPath = ReadLine();
Console.Write("Do you want detailed information displayed during the copy process? ");
string response = ReadLine();

if (response?.Substring(0, 1).ToUpper() == "Y")
{
    _detailedReport = true;
}

Upvotes: 2

Blake Thingstad
Blake Thingstad

Reputation: 1659

You can create a function to get the user's input and after the Console.ReadLine() just do any processing (exiting on 'q') on the input before returning it.

static void Main(string[] args)
{
    Console.Write("Enter source path: ");
    var _sourcePath = GetInput();

    Console.Write("Enter destination path: ");
    var _destinationPath = GetInput();

    Console.Write("Do you want detailed information displayed during the copy process? ");
    var response = GetInput();

    var _detailedReport = response?.Substring(0, 1)
        .Equals("y", StringComparison.CurrentCultureIgnoreCase);
}

private static string GetInput()
{
    var input = Console.ReadLine();
    if (input.Equals("q", StringComparison.CurrentCultureIgnoreCase))
        Environment.Exit(Environment.ExitCode);
    return input;
}

Upvotes: 2

Related Questions