Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#控制台应用程序中编辑文本?_C#_.net_Command Line_Console_Console Application - Fatal编程技术网

在C#控制台应用程序中编辑文本?

在C#控制台应用程序中编辑文本?,c#,.net,command-line,console,console-application,C#,.net,Command Line,Console,Console Application,有没有办法在C#控制台应用程序中编辑文本?换句话说,是否可以在命令行上放置预定义的文本,以便用户可以修改文本,然后重新提交到应用程序?是。您需要使用控制台的SetCursorPosition方法。例如: Console.WriteLine("hello"); Console.SetCursorPosition(4, 0); Console.WriteLine(" "); 它将显示“地狱” 您需要ReadLine方法的自定义实现,该方法允许您在控制台中编辑n个符

有没有办法在C#控制台应用程序中编辑文本?换句话说,是否可以在命令行上放置预定义的文本,以便用户可以修改文本,然后重新提交到应用程序?

是。您需要使用控制台的SetCursorPosition方法。例如:

    Console.WriteLine("hello");
    Console.SetCursorPosition(4, 0);
    Console.WriteLine("      ");
它将显示“地狱” 您需要ReadLine方法的自定义实现,该方法允许您在控制台中编辑n个符号(默认字符串)并从用户返回字符串。这是我的例子:

static string ReadLine(string Default)
{
    int pos = Console.CursorLeft;
    Console.Write(Default);
    ConsoleKeyInfo info;
    List<char> chars = new List<char> ();
    if (string.IsNullOrEmpty(Default) == false) {
        chars.AddRange(Default.ToCharArray());
    }

    while (true)
    {
        info = Console.ReadKey(true);
        if (info.Key == ConsoleKey.Backspace && Console.CursorLeft > pos)
        {
            chars.RemoveAt(chars.Count - 1);
            Console.CursorLeft -= 1;
            Console.Write(' ');
            Console.CursorLeft -= 1;

        }
        else if (info.Key == ConsoleKey.Enter) { Console.Write(Environment.NewLine); break; }
        //Here you need create own checking of symbols
        else if (char.IsLetterOrDigit(info.KeyChar))
        {
            Console.Write(info.KeyChar);
            chars.Add(info.KeyChar);
        }
    }
    return new string(chars.ToArray ());
}
静态字符串读取行(字符串默认值)
{
int pos=Console.CursorLeft;
Console.Write(默认);
ConsoleKeyInfo信息;
列表字符=新列表();
if(string.IsNullOrEmpty(默认值)=false){
AddRange(默认为.tocharray());
}
while(true)
{
info=Console.ReadKey(true);
if(info.Key==ConsoleKey.Backspace&&Console.CursorLeft>pos)
{
字符移除(字符计数-1);
Console.CursorLeft-=1;
控制台。写入(“”);
Console.CursorLeft-=1;
}
如果(info.Key==ConsoleKey.Enter){Console.Write(Environment.NewLine);break;}
//这里您需要创建自己的符号检查
else if(char.isleterordigit(info.KeyChar))
{
Console.Write(info.KeyChar);
添加字符(info.KeyChar);
}
}
返回新字符串(chars.ToArray());
}

此方法将显示字符串默认值。希望我对你的问题理解正确(我对此表示怀疑)

我想到的一件事是……模拟击键。 还有一个使用SendKeys的简单示例:

static void Main(string[] args)
{
    Console.Write("Your editable text:");
    SendKeys.SendWait("hello"); //hello text will be editable :)
    Console.ReadLine();
}

注意:这仅适用于活动窗口。

我不认为我的问题与提供的问题重复。我的需要要具体得多,关于这个问题的答案并不能回答这个问题。据我所知,Console类没有任何东西可以帮助在命令行上放置可编辑文本。在控制台应用程序中没有命令行,除非您编写了一个命令行。这可以通过Console类完成。Edit:cmd行是shell,用于启动控制台应用程序。当它正在运行并且您正在将内容输出到控制台窗口时,您没有使用shell命令,即命令行。您将解释已按下的键,并根据键执行特定操作(即,按下backspace时将光标向后移动一个字符)。'@Alex Ford:关于“更具体”——“如何打印int”比“如何打印对象”更具体,看不出有什么不同。我不确定你是否应该编辑命令行中的文本。。。这毕竟是一条命令:你能澄清一点吗?我不太明白。我将此代码放在一个新的控制台应用程序中,它确实显示“地狱”,但这如何帮助我编辑命令行上的文本?+1,因为不必引用System.Windows.FormsGreat answer。对于那些有SendKeys问题的人来说非常有用!你的方法比我的简单。似乎我喜欢发明自行车:))你的代码真的很好,你没有发明自行车,因为在我建议的解决方案中,需要使用System.Windows.Forms命名空间或PInvoke SendInput或其他熟悉的东西+从我这里:)谢谢。我们都认为彼此的方法很好。正如在另一个问题中指出的那样,你需要注意你的应用程序是有重点的。要避免这些问题,请查看答案。