How can I search if content in one text file appears in another text file in C# [duplicate]

StackOverflow https://stackoverflow.com/questions/23298501

  •  09-07-2023
  •  | 
  •  

문제

I am writing a code that allows a user to brows a text file and then check if the content in that text file appears in another text file.

Currently I have this code.

DialogResult result = openFileDialog1.Show()
If (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
try
{
StreamReader sr = new StreamReader(FilePath);
String Data = sr.ReadToEnd();
sr.Close();

string text = File.ReadAllText(file);

If (Data == text)
{
MessageBox.Show("This files are the same", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("This files are not the same", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (IOException ie)
{
MessageBox.Show(ie.ToString(), "Exception Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

This can only check if the text files are the same but i also want to check if the content in the text file the user browses for appear in the text file I read in my streamRead, how can I do that?

도움이 되었습니까?

해결책

if you want to check the file1 contains file2 data

Replace This:

if (Data == text)

With This:

if (Data.Contains(text))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top