【问题标题】:How to display a text file into console after user input? in C#用户输入后如何在控制台中显示文本文件?在 C# 中
【发布时间】:2013-08-20 15:36:58
【问题描述】:

我有 3 个不同的记事本文件,Jack.txt Ken.txtWizard.txt 当我想在程序中输入时,这在编码中是如何工作的,例如,我输入 Ken 并且程序应该加载 Ken.txt 文件, JackJack.txt 等等。

下面的编码是非常基本的,目前尚未完成,但无论我输入什么,它都会加载“Jack.txt”文件。如果我将编码分成循环,这是否可行,所以当我输入“Wizard”时,它将循环直到他们找到 Wizard.txt 文件,如果没有出现错误消息。

    //Prompt for input
    System.Console.WriteLine("Please enter the name");
    System.Console.Write("Name> ");
        string name = System.Console.ReadLine();

    // Fetch the file from input
    string text = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Jack.txt");
    string text1 = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Ken.txt");
    string text2 = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Wizard.txt");

    // Display the attributes to the console.
    System.Console.WriteLine(" ");
    System.Console.WriteLine("{0}", text);


  }
 }
}

【问题讨论】:

  • 您的代码几乎就在那里,但如果没有换行符,它将无法阅读
  • 您可以添加一个 switch 语句来检查用户输入的内容,然后根据用户输入的匹配“Jack”、“Ken”或“Wizard”的内容加载正确的文件...参见msdn.microsoft.com/en-us/library/06tc147t(v=vs.90).aspx

标签: c# file search console notepad


【解决方案1】:

您想使用“if”语句来决定是打印一个还是另一个:

if (name == "Jack") {
    Console.WriteLine(text);
}
else if (name == "Ken") {
     ...
}

【讨论】:

  • 或者,使用 string.Format("c:\\your\\path\\here\\{0}", inputString) 作为位置参数,让他们输入他们想要的任何名称。
【解决方案2】:

它不仅会加载 Jacks 的所有文件,而且由于您已将 text 变量硬编码到输出中,并且它引用 Jack 文件,因此它是您看到的唯一文件。

但是,如果您想根据用户输入的名称在这三个之间进行选择,则可以按需要进行操作:

string name = System.Console.ReadLine();
string textContent = "";
string dir = @"D:\Users\Jack\Documents\Test"; 
if(name.Equals("Jack", StringComparison.OrdinalIgnoreCase))
{
    textContent = File.ReadAllText(Path.Combine(dir, "Jack.txt"));
}
else if(name.Equals("Ken", StringComparison.OrdinalIgnoreCase))
{
    textContent = File.ReadAllText(Path.Combine(dir, "Ken.txt"));            
}
else if(name.Equals("Jack", StringComparison.OrdinalIgnoreCase))
{
    textContent = File.ReadAllText(Path.Combine(dir, "Wizard.txt"));
}
else
{
     // output error or ask for another name
}
System.Console.WriteLine(" ");
System.Console.WriteLine("{0}", textContent);

【讨论】:

  • 我认为在 switch 语句中会更简洁。
  • 我试过你的编码 Tim,但是 textContent = File.ReadAllText(Path.Combine(dir, "Jack.txt"));编码显示错误“文件”和“路径”在当前上下文中不存在。这意味着 Ken.txt 和 Wizard.txt 相同
  • @DotNetHaggis:如果需要,开关不支持不区分大小写的字符串比较(例如)(请参阅我的回答)。
  • @TimSchmelter 标记的答案似乎是使用开关。只是看起来更干净,但我同意不区分大小写的字符串。你可以只使用 ToLower()。
  • @TimSchmelter 谢谢。我已经学会了为什么使用和不使用某些方法。由于这个理由,我对你的答案投了赞成票。
【解决方案3】:

类似这样的:

//Prompt for input
System.Console.WriteLine("Please enter the name");
System.Console.Write("Name> ");

string name = System.Console.ReadLine();
string text;
if (new[] {"Jack", "Ken", "Wizard"}.Contains(name))
{
    // Fetch the file from input
    text = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\" + name + ".txt");
}

// Display the attributes to the console.
System.Console.WriteLine("");
System.Console.WriteLine("{0}", text);

【讨论】:

    【解决方案4】:

    你必须使用条件语句(if-else、switch 等)

    请参阅下面的代码以获取示例。您可以随意编辑。

            //Prompt for input
            System.Console.WriteLine( "Please enter the name" );
            System.Console.Write( "Name> " );
            string name = System.Console.ReadLine();
    
            /*
             *  Notice how I don't load the files here?
             *  If one of the files is 100 MB, your program will use 100 MB of memory and possibly more.
            */
            string text;
    
            //Display the attributes to the console.
            System.Console.WriteLine( " " );
    
    
            // Add a conditional switch statement.
            switch ( name ) // This is similar to using if-else statements.
            {
                case "Jack":
                    text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Jack.txt" );
                    Console.WriteLine( text );
    
                    break; // This is used to leave the switch statement.
    
                case "Ken":
                    text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Ken.txt" );
                    Console.WriteLine( text );
    
                    break;
    
                case "Wizard":
                    text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Wizard.txt" );
                    Console.WriteLine( text );
    
                    break;
    
                default: // This is when the program can't match any values above.
                    Console.WriteLine( "Error! No-one exists with that name!" );
    
                    break;
            }
    

    【讨论】:

    • 谢谢,我已经尝试过了,现在可以使用了。虽然我必须稍微编辑一下编码,因为我可能需要添加更多的 .txt 文件!
    猜你喜欢
    • 2018-07-13
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多