【发布时间】:2013-08-20 15:36:58
【问题描述】:
我有 3 个不同的记事本文件,Jack.txt Ken.txt 和 Wizard.txt 当我想在程序中输入时,这在编码中是如何工作的,例如,我输入 Ken 并且程序应该加载 Ken.txt 文件, Jack 为 Jack.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