July 24th, 2009 | |
Posted in C#
This one is pretty self explanatory. You have some content you’ve gathered from a database, webservice, other means and you want to write that out to a file. We are going to call on the StreamWriter class for help. First things first, reference the System.IO class so you have all the libraries to make this work. Next, we define what the file is going to be called and where it is going to be written to. When I define the StreamWriter, I’m passing in three values 1) the file path, 2) whether I want to append the content to the existing file (true) or overwrite it (false), 3) and what form of encoding I’ll be using (UTF8 in the instance). Next, we go through and write each line out and flush it into the file. Five lines of code to take care of this indispensable function.