Skip to content

Copy and Edit Data in File

Dennis

Asked Jan 14, 2008, 6:14 AM EST2 answers
Share
LinkedInX
I've just started C# and have little experience with programming. I've browsed trhough many topics in this forum but none seem to be exactly what I'm looking for...

I want to create a tool which compares to files, not in bytesize but in by content. I've managed to create a tool who does so, but he compares complete files. I'm unable to create a way the tool stops when a certain phrase comes up. So I had the idea to create two temporary files which I compare, but here I have the same problem. I'm unable to copy a certain block of data.

Is there anybody who can help me with my problem?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Here is something that may help you out with the reading and comparing of the file contents and notifying the user that it ran into a line of data that was missing in the other file:

    private bool AreFilesEqual(string filePathOne, string filePathTwo)
    {
        StreamReader fileOneReader = new StreamReader(new                         FileStream(filePathOne, FileMode.Open));
        StreamReader fileTwoReader = new StreamReader(new                         FileStream(filePathTwo, FileMode.Open));
       
        if(fileOneReader.ReadToEnd() != fileTwoReader.ReadToEnd())
           return false;
        else
            return true;
    }

    Im not quite sure though what you are trying to copy. Do you want to copy the line of data that is in one file and not the other to the file that is missing it?
    0
  • 0
    This is almost the code I have now. It works so, I'm able to compare to files by its complete content. But I want to compare a specific part in the file. For instance from the beginning of the file untill a certain phrase/line in the file. The problem is that this phrase can come up in any random line, so it's not not possible to define a line-number.

    private bool AreFilesEqual(string filePathOne, string filePathTwo)
    {
        StreamReader fileOneReader = new StreamReader(new                         FileStream(filePathOne, FileMode.Open));
        StreamReader fileTwoReader = new StreamReader(new                         FileStream(filePathTwo, FileMode.Open));
       
        if(fileOneReader.ReadToEnd() != fileTwoReader.ReadToEnd())
           return false;
        else
            return true;
    }

    Because of the ReadToEnd() it compares the complete file. This is not what I want.
    0