Tuesday, 3 March 2009

Writing files to disc

This is a simple enough procedure but, in honesty, I always, always forget how to do it, and when you go and look it up on the interweb it seems that the examples provided are far more complicated than they really need to be. I guess it must be that the System.IO namespace simply isn't as intuitive as perhaps it should be. So here's the stripped down, basic code you need to write a file to disc - don't forget that the namespace goes at the top of your file, while the actual operational code goes inside it.

using System.IO;

FileStream myStream = new FileStream("insert File path here", FileMode.Create);
StreamWriter myWriter = new StreamWriter(myStream);

myWriter.Write("Some Text");
//this creates a new line. Duh!


So put the target path into "insert File path here" and put your text into where it says "some text". Obviously you can keep adding as much text or new lines as you need.

No comments:

Post a Comment