如何在用户输入后将文本文件显示到控制台?在C#中

时间:2013-08-18 21:04:40

标签: c# file search console notepad

我有3个不同的记事本文件,Jack.txt Ken.txtWizard.txt当我想在程序中输入时,如何在编码中工作,我输入Ken和该程序应加载Ken.txt文件,Jack加载Jack.txt,依此类推。

下面的编码是非常基本的,目前尚未完成,但无论我输入什么,都会加载“Jack.txt”文件。如果我将编码分成循环,这是否有效,所以当我输入“向导”时,它将循环,直到找到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);


  }
 }
}

4 个答案:

答案 0 :(得分:1)

它不仅加载所有文件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);

答案 1 :(得分:1)

这样的事情:

//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);

答案 2 :(得分:0)

您想使用'if'语句来决定是否打印其中一个:

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

答案 3 :(得分:0)

您必须使用条件语句(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;
        }