Whidbey BCL the Easy Way #3: One line File Reading

Here is another item that I am putting in my BCL talk. It’s not earth shattering but it’s sure to bring a round of applause! 🙂

I have a presentation I have done a few times on Streaming in .NET Tips and Tricks. Trying to explain the ins and outs of streamreaders and streamwriters, that they are not containers – just transporters, that you use Read to write and Write to read (think about it…). All VERY confusing to non-plumbers. If you are using streaming to open a file up and see it’s contents or create file, you have a lot of concepts to understand and a lot of lines of code. And then don’t forget to close the file stream or is it close the file or …oh god. I better go look at my slides again.

Which is why, even if it is one of the more seemingly pedestrian changes in .NET, File.ReadAll and File.WriteAll are very welcome new methods in the Base Classes.

With this one short line of code:

Dim str as string=File.ReadAll(“c:\mynicefile.txt”)

(and you can do this in C# too and in this.net and that.net cause it’s in the BCL…)

I now have a string filled with the contents of my text file. I don’t even have to instantiate an object. I don’t have to close any resources.

No research, no acrobatics.

One very good point that Kit George makes about this is that it’s intuitive. Certainly you have started by typing “File.” and looked up and down the intellisense list for something that would do this. Well, now it’s there. Thanks guys and gals!

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

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.