I have been using the .NET Framework heavily since 2002, and sometimes I have to sit back an marvel at a good, well-designed the API. I say this because just today I had to write some code to write to the file system, and it’s remarkable how simple it is to create a text file and write information to it.  You can all do it in just 2 lines of code using the System.IO.File class. Simply put, this class is a façade (or a wrapper) around the classes in the .NET Framework that work together to provide access to the file system on a Windows machine.  All the methods are static (shared for VB.NET) and it provides the most common functionality that you would normally need from the file system. Here is a little code to show you what I mean:

 
   1:  using System;
   2:  using System.IO;
   3:  using System.Text;
   4:   
   5:  namespace SimpleFileWriter {
   6:      class Program {
   7:          static void Main( string[ ] args ) {
   8:              using (StreamWriter writer = File.CreateText( @"c:\temp\test.txt" ))
   9:                  writer.WriteLine( "Hello World!" );
  10:   
  11:          }
  12:      }
  13:  }
 
 

I have posted the entire program just for your reference; but only lines 8 and 9 are needed to write data to the file system.  You can find more information on the other members of the File class here. I’m sure this may not be new to some of you; but the intent of this post was to emphasize how a well-designed API goes a long way. Until next time…

Happy Coding!

 

kick it on DotNetKicks.com

Bookmark and Share