![]() ![]() You can type in "File." in your IDE (make sure you include the System.IO import statement) and see all the methods available. The old way, which I was taught in college was to use stream reader/stream writer, but the File I/O methods are less clunky and require fewer lines of code. If the file doesn't exist, it will create it. These are the best and most commonly used methods for writing to and reading from files: using System.IO įile.AppendAllText(sFilePathAndName, sTextToWrite) //add text to existing fileįile.WriteAllText(sFilePathAndName, sTextToWrite) //will overwrite the text in the existing file. I would never have found this myself, but it works great, so I wanted to share this. This is how to use the string extension method, note that it refers automagically to the class Strings: using Lib //(extension) method(s) for string ![]() of course you could add other useful string methods. Public static void WriteToFile(this string Str, string Filename) / Extension method to write the string Str to a file The class name does not matter the class and method must be declared static. Note that the only thing that really matters is the function argument with extra keyword this, that makes it refer to the object that the method is attached to. This defines the extension method on the string type. It takes just a little coding, then provides the absolute easiest way to read/write, plus it offers the flexibility to create variations according to your personal needs. Pointed me at another "easiest way" namely the extension method. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |