C# streamreader使用文本框时出现问题

C# streamreader使用文本框时出现问题,c#,C#,我是C#新手,编写这个应用程序,如果文本框中的给定名称在流行列表文件中,它会显示一条消息。我的书对解决这个问题几乎没有帮助,我的错误是if(boy.Contains(boyinputFile))的inputfile对于女孩来说是一样的,其中提到: 无法转换为字符串 更换这条线 if (boyBox.Text.Contains(boyinputFile)) 与 试试这个: string boy = boyBox.Text; using (StreamReader sr = new Stream

我是C#新手,编写这个应用程序,如果
文本框中的给定名称在流行列表文件中,它会显示一条消息。我的书对解决这个问题几乎没有帮助,我的错误是
if(boy.Contains(boyinputFile))
的inputfile对于女孩来说是一样的,其中提到:

无法转换为字符串

更换这条线

if (boyBox.Text.Contains(boyinputFile))

试试这个:

string boy = boyBox.Text;

using (StreamReader sr = new StreamReader("D:\\BoyNames.txt"))
{
      string boyinputFile = sr.ReadToEnd();

      if (boyinputFil.Contains(boy))
      {
           MessageBox.Show("Yes, your name is popular!");
      }
      else
      {
           MessageBox.Show("Sorry, couldn't find your name.");
      }
}

您需要将流转换为字符串。这就是您得到错误的地方:

无法转换为字符串

然后需要查看所选名称是否在字符串文件中。你需要退票。您正在检查文本框是否包含该文件

if (girl_file.Contains(girl))
{
            MessageBox.Show("Yes, your name is popular!");
}

还可以看看这个问题

我想为您提供比现有答案更好的代码

var lines = File.ReadAllLines("...");
var isMatch = lines.Contains(name);

它真的可以这么简单。

看一看。它应该会给你关于如何修复编码错误的灵感。我看你是新手。如果任何一个答案都解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,您已经找到了解决方案。
string girl_file = streamReader.ReadToEnd();
if (girl_file.Contains(girl))
{
            MessageBox.Show("Yes, your name is popular!");
}
var lines = File.ReadAllLines("...");
var isMatch = lines.Contains(name);