如何在C#控制台中输入文本而不是用户输入?

5
在我的C#控制台应用程序中,我提示用户插入IP地址:
string strIpAddress;
Console.WriteLine("Type the IP Address:");
strIpAddress = Console.ReadLine();

输出结果如下所示:

enter image description here

我希望将默认的IP地址文本放在控制台上,让用户看到并按ENTER键。如果默认IP无效,则用户应该能够删除文本(用退格键),更正IP地址,然后按ENTER键。用户应该看到类似以下内容:

enter image description here

我不知道如何做到这一点! ;-(

谢谢任何建议。


1
我认为这是一个重复的问题:https://dev59.com/lXI-5IYBdhLWcg3w0cKG - evilkos
一个想法浮现在脑海中,就是将键盘进行钩取,并模拟自己输入,但某种直觉告诉我这可能会有些过度。 - Abion47
4
一般而言,最简单的方法是告诉用户如果按回车键会选择哪个默认选项,例如“请输入IP地址[192.168.1.1]:”,其他方案就不那么容易了,因为你必须模拟输入或自行构建编辑器。 - Lasse V. Karlsen
Maximilian Gerhardt在下面的回答中提供的纯C#方法可能是你能得到的最简单的方法。如果你想尝试P/Invoke,这里可能有一些你可以使用的内容:http://www.pinvoke.net/default.aspx/kernel32/ConsoleFunctions.html - Abion47
2个回答

5

编辑以允许用户进行编辑

Console.Write("Type the IP Address:\n");
SendKeys.SendWait("192.168.1.1"); //192.168.1.1 text will be editable :)
strIpAddress=Console.ReadLine();

这需要将 System.Windows.Forms 添加到引用并添加命名空间。

2
好的,这种方式默认值是不可编辑的。但它的优点可能是唯一的简单半解决方案。 - evilkos
嗯,我实际上是打算将它用于这样一个“默认”的目的。 - user3598756
感谢你的回答,亲爱的@user3598756。但是它不能由用户进行编辑。这样我应该提示用户:如果这是 IP 地址,请按回车键。如果不是,请自行输入然后按回车键!;-) - Shamim
见我从这里编辑的答案。 - user3598756
哇,这太有趣了! - Shamim
非常好,这里完全正常!现在它是完整的解决方案,比任何其他建议都要简单得多。 - TaW

4

以下是一个更复杂的示例,使用Console.SetCursorPosition()将光标向左移动(如果可能),并使用Console.ReadKey()直接读取按键以拦截退格键和回车键:

using System;
using System.Linq;

namespace StackoverflowTests
{
    class Program
    {

        public static void Main(string[] args)
        {
            Console.WriteLine("Type the IP Address: ");
            //Put the default IP address 
            var defaultIP = "192.168.0.190";
            Console.Write(defaultIP);

            string input = defaultIP;
            //Loop through all the keys until an enter key
            while (true)
            {
                //read a key
                var key = Console.ReadKey(true);
                //Was this is a newline? 
                if (key.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine();
                    break;
                }
                //Was is a backspace? 
                else if (key.Key == ConsoleKey.Backspace)
                {
                    //Did we delete too much?
                    if (Console.CursorLeft == 0)
                        continue; //suppress
                    //Put the cursor on character back
                    Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                    //Delete it with a space
                    Console.Write(" ");
                    //Put it back again
                    Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                    //Delete the last char of the input
                    input = string.Join("", input.Take(input.Length - 1));
                }
                //Regular key? add it to the input
                else if(char.IsLetterOrDigit(key.KeyChar))
                {
                    input += key.KeyChar.ToString();
                    Console.Write(key.KeyChar);
                } //else it must be another control code (ESC etc) or something.
            }

            Console.WriteLine("You entered: " + input);
            Console.ReadLine();
        }
    }
}

如果您想添加对左箭头和右箭头按键的支持,甚至是召回上次输入内容的上箭头按键,此功能可以进一步提高。

这可能是最“简单”的事情了。 - Abion47
谢谢@Maximilian,我已经测试了你的代码,它运行得非常好。我以后唯一需要添加的是避免非字符按键,比如“ArrowUp”或“ESC”。虽然我希望在我不知道的.NET控制台库中有一些预定义的方法! - Shamim
@Shamim 你可以添加一个 else if(char.IsLetterOrDigit(key.KeyChar)) 来捕获这些键,我一开始没有做这个是我的错。 - Maximilian Gerhardt
没问题,但我觉得出于简洁起见我会采用user3598756的答案! - Shamim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接