Loading text file contents into a string

While working on some XSD validation code today, I found that I needed to load a couple of text files into strings to unit test. I’d forgotten how I’d done this before, but I googled the answer with this search term:

file to string .net

The top result (at least as of today), gave me the answer I needed. I’ve reproduced it as the following function:

Update: Ed Poore let me know in a comment that the .NET Framework contains a method that does this already.  System.IO.File.ReadAllText(path)  does the same thing, so you can completely ignore the method above.


  1. Ed Poore says:

    Not a problem, no one can know then entire contents of the framework. Sometimes the strategy is to try and think like the developers and think how they would name something and Google it.

    Glad I could be of some service.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.