5StringRyan
5StringRyan

Reputation: 3634

C#: Compare text file contents to a string variable

I have an application that dumps text to a text file. I think there might be an issue with the text not containing the proper carriage returns, so I'm in the process of writing a test that will compare the contents of of this file to a string variable that I declare in the code.

Ex:

1) Code creates a text file that contains the text:

This is line 1
This is line 2
This is line 3

2) I have the following string that I want to compare it to:

string testString = "This is line 1\nThis is line 2\nThis is line3"

I understand that I could open a file stream reader and read the text file line by line and store that in a mutable string variable while appending "\n" after each line, but wondering if this is re-inventing the wheel (other words, .NET has a built in class for something like this). Thanks in advance.

Upvotes: 3

Views: 8476

Answers (6)

DotNetUser
DotNetUser

Reputation: 6612

you can either use StreamReader's ReadToEnd() method to read contents in a single string like

    using System.IO;

    using(StreamReader streamReader = new StreamReader(filePath))
    {
       string text = streamReader.ReadToEnd();
    }

Note: you have to make sure that you release the resources (above code uses "using" to do that) and ReadToEnd() method assumes that stream knows when it has reached an end. For interactive protocols in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely because it does not reach an end, and should be avoided and also you should take care that current position in the string should be at the start.

You can also use ReadAllText like

    // Open the file to read from.
    string readText = File.ReadAllText(path);

which is simple it opens a file, reads all lines and takes care of closing as well.

Upvotes: 5

Ry-
Ry-

Reputation: 224974

if(System.IO.File.ReadAllText(filename) == "This is line 1\nThis is line 2\nThis is line3") {
   // it matches
}

Upvotes: 1

Arun
Arun

Reputation: 2523

You can read the entire file into a string variable this way:

        FileStream stream;
        StreamReader reader;
        stream = new FileStream(yourFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        reader = new StreamReader(stream);
        string stringContainingFilesContent = reader.ReadToEnd();
        // and check for your condition
        if (testString.Equals(stringContainingFilesContent, StringComparison.OrdinalIgnoreCase))

Upvotes: 0

Chris Shain
Chris Shain

Reputation: 51349

No, there is nothing built in for this. The easiest way, assuming that your file is small, is to just read the whole thing and compare them:

var fileContents = File.ReadAllText(fileName);
return testString == filecontents;

If the file is fairly long, you may want to compare the file line by line, since finding a difference early on would allow you to reduce IO.

Upvotes: 4

kmcc049
kmcc049

Reputation: 2801

A faster way to implement reading all the text in a file is System.IO.File.ReadAllText()

but theres no way to do the string level comparison shorter

Upvotes: 1

adamjmarkham
adamjmarkham

Reputation: 2164

This should work:

StreamReader streamReader = new StreamReader(filePath);
string originalString = streamReader.ReadToEnd();
streamReader.Close();

I don't think there is a quicker way of doing it in C#.

Upvotes: 0

Related Questions