.Contains()文件内容无法按预期工作

时间:2015-09-22 12:42:35

标签: c#

我正在尝试创建一个ai。我有把它写成一个带有信息的主题到文本文件,但无法让它阅读。这就是我到目前为止所拥有的。关于它为什么不起作用的任何想法?

string information = "blank";
Console.WriteLine("Hello my name is Lou-C.");
Console.WriteLine("What is yours?");
string name = Console.ReadLine();
Console.WriteLine("Hello {0}, What do you want to talk about?", name);
string topic = Console.ReadLine();
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\simon.light\Documents\Visual Studio 2013\Projects\Lou-c\Lou-c\information.txt");
if (lines.Contains("beat"))
{
    Console.WriteLine("Aaahh, {0}, I have heard of this before");
    Console.WriteLine("Tell me what you know about this");
    information = Console.ReadLine();
}
else
{
Console.WriteLine("{0}? I don't think i have come across this before.", topic);
Console.WriteLine("Tell me about it");
information = Console.ReadLine();
}

using (System.IO.StreamWriter file = 
new System.IO.StreamWriter(@"C:\Users\simon.light\Documents\Visual Studio 2013\Projects\Lou-c\Lou-c\information.txt", true))
{
    file.WriteLine("${0} - %{1} - by {2}", topic, information, name);
} 

1 个答案:

答案 0 :(得分:0)

检查包含" beat"

的任何行
if (lines.Any(s => s.Contains("beat")))

但是,如果你使用了ReadAllText,那么你的包含就可以了!

string text = System.IO.File.ReadAllText(...);
if (text.Contains("beat"))
相关问题