How to set default input value in .Net Console App?

前端 未结 8 1381
时光取名叫无心
时光取名叫无心 2020-12-11 16:01

How can you set a default input value in a .net console app?

Here is some make-believe code:

Console.Write("Enter weekly cost: ");
string inp         


        
8条回答
  •  囚心锁ツ
    2020-12-11 16:21

    Here's a simple solution:

    public static string ConsoleReadLineWithDefault(string defaultValue)
    {
        System.Windows.Forms.SendKeys.SendWait(defaultValue);
        return Console.ReadLine();
    }
    

    It's not complete however. Some characters in the SendWait input string have special meaning so you have to escape them (eg. +, (, ), etc.) See: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx for a complete description.

提交回复
热议问题