-1

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?

Donald Dax
  • 63
  • 10
  • 2
    Quite a broad question, and your task is slightly undefined. What do you mean by "appear" - single words, or the whole content? If the first, you need to define words. If the latter, just use `Contains`. Note also that reading whole files into memory is in general not a good approach - unless you can guarantee that the files are small. – BartoszKP Apr 25 '14 at 16:36
  • what is the problem with existing code. – Sudhakar Tillapudi Apr 25 '14 at 16:37
  • @BartoszKP I'm trying to check whether the whole content in one text file appears in the other one. I tried using Data.Contains(File) but wen I run the application, even if I use two different files with the same content the application doesn't do what it's supposed to do. – Donald Dax Apr 25 '14 at 17:11
  • @SudhakarTillapudi, the code I have here one checks if the files are the same, I want to read if all the content in the text file I brows for appear in the other text file. I'm not really sure how I can get that – Donald Dax Apr 25 '14 at 17:13
  • @user3251829: does your code compile? – Sudhakar Tillapudi Apr 25 '14 at 17:15
  • @SudhakarTillaudi yes the code compiles, I don't get any errors, the problem is that it only checks if the files are the same, I actually want to check if the entire content appears in the other text file. – Donald Dax Apr 25 '14 at 17:20
  • @user3251829: you need to use `Contains()`, try this-> `if (Data.Contains(text))` , check my answer . – Sudhakar Tillapudi Apr 25 '14 at 17:22
  • @user3251829 that's because `Data.Contains(File)` uses the `File` variable which doesn't hold the contents of your file. You meant perhaps `Data.Contains(text)`... – BartoszKP Apr 25 '14 at 17:40
  • @BartoszKP thanks, I realized that, it's just that my actual application uses different names then the one I posted here, but thanks you guys have helped put, finally my code is doing what I want from it – Donald Dax Apr 25 '14 at 17:48

1 Answers1

-3

if you want to check the file1 contains file2 data

Replace This:

if (Data == text)

With This:

if (Data.Contains(text))
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • 3
    This answer is loads of nonsense, especially that ReadLines note (it's not async, it does not free up the thread). Also, using Equals to compare strings is dumb (because == calls Equals), C# isn't Java. Also comparing file contents is not finding a subset. – Cat Plus Plus Apr 25 '14 at 16:51
  • @BartoszKP: edited my answer asper OP comments – Sudhakar Tillapudi Apr 25 '14 at 17:23
  • @SudhakarTillapudi Wow, great, impressive (lol)! I removed my downvote, but you still need to account for downvotes from all the people who saw the type of "job" you're doing here, downvoted and left. Hope next time you'll take some time to think before you post. – BartoszKP Apr 25 '14 at 17:31
  • @BartoszKP: Thanks for your kind words. Yea Sure i will definatly think twice before posting answers from now onwards:) , Thanks – Sudhakar Tillapudi Apr 25 '14 at 17:41
  • @SudhakarTillapudi thanks for that, now my code works exactly the way I want it. – Donald Dax Apr 25 '14 at 17:41
  • @BartoszKp thanks for your help as well, u guys have been a great help – Donald Dax Apr 25 '14 at 17:43