用户输入文件路径,标签自动完成

时间:2016-08-19 01:35:16

标签: c# cmd console-application

我现在正在接受用户输入文件路径:

Console.WriteLine("Input file path");
string path = Console.ReadLine();

try
{
    data = System.IO.File.ReadAllBytes(path);
}
catch
{
    Console.WriteLine("Invalid file path entered");
    System.Console.ReadKey();
    return 1;
}

但是如果用户错误地输入了路径,他们就必须再次输入整个路径。我意识到目前我的应用程序将在用户输入错误时退出,我可以再次询问,但我仍然希望让用户更轻松一点。

相反,我想在用户输入时选中路径时,为路径自动完成Windows命令行功能。例如,如果我打开cmd并键入cd C:\win并点击TAB,cmd将找到C:\ Windows。

是否可以将该功能添加到控制台应用程序以供用户输入?

3 个答案:

答案 0 :(得分:3)

起初我认为清除特定的控制台线是不可行的,但快速搜索显示nothing is impossible

所以我创建了一个新的控制台应用程序,并开始考虑如何使用这样的工具。以下是第一份工作草案" - 我即将[重度]自行重构它,然后将生成的代码放在Code Review上,但这应该足以让你开始。

程序使 Tab 键使用字符串数组作为数据自动完成当前输入,匹配它找到的第一个项目;如果你想要更聪明的东西(例如将当前文件夹的子路径作为数据,和/或在每次连续按 Tab 关键):

class Program
{
    static void Main(string[] args)
    {
        var data = new[]
        {
            "Bar",
            "Barbec",
            "Barbecue",
            "Batman",
        };

        var builder = new StringBuilder();
        var input = Console.ReadKey(intercept:true);

        while (input.Key != ConsoleKey.Enter)
        {
            var currentInput = builder.ToString();
            if (input.Key == ConsoleKey.Tab)
            {
                var match = data.FirstOrDefault(item => item != currentInput && item.StartsWith(currentInput, true, CultureInfo.InvariantCulture));
                if (string.IsNullOrEmpty(match))
                {
                    input = Console.ReadKey(intercept: true);
                    continue;
                }

                ClearCurrentLine();
                builder.Clear();

                Console.Write(match);
                builder.Append(match);
            }
            else
            {
                if (input.Key == ConsoleKey.Backspace && currentInput.Length > 0)
                {
                    builder.Remove(builder.Length - 1, 1);
                    ClearCurrentLine();

                    currentInput = currentInput.Remove(currentInput.Length - 1);
                    Console.Write(currentInput);
                }
                else
                {
                    var key = input.KeyChar;
                    builder.Append(key);
                    Console.Write(key);
                }
            }

            input = Console.ReadKey(intercept:true);
        }
        Console.Write(input.KeyChar);
    }

    /// <remarks>
    /// https://stackoverflow.com/a/8946847/1188513
    /// </remarks>>
    private static void ClearCurrentLine()
    {
        var currentLine = Console.CursorTop;
        Console.SetCursorPosition(0, Console.CursorTop);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, currentLine);
    }
}

感谢您提出这个问题,这很有趣!

答案 1 :(得分:1)

如果其他人有此用例,我将添加Mathieu Guindon出色的answer

如果您想获得一个提示来邀请用户做出选择,并且希望能够保留提示并仅循环显示替换每个选项名称的选项,那么您要做的就是更改{{ 1}}方法,使其看起来像这样:

ClearCurrentLine

然后更改呼叫者以传递private static void ClearCurrentLine(int cursorLeft) { var currentLine = Console.CursorTop; Console.SetCursorPosition(cursorLeft, Console.CursorTop); Console.Write(new string(' ', Console.WindowWidth - cursorLeft)); Console.SetCursorPosition(cursorLeft, currentLine); } 的适当值。哑巴示例:

cursorLeft

通过这种方式,“选择”不会被该方法删除。

您可以只再次打印一次提示,但是感觉有点干净。

答案 2 :(得分:0)

你应该能够实现它。虽然它不是直接的,可能没有现成的库,但您可以使用以下内容:

{{1}}

可能对您有所帮助的一些链接:

C# Console - set cursor position to the last visible line

https://msdn.microsoft.com/en-us/library/system.console.setcursorposition(v=vs.110).aspx

相关问题