Description (2017-7-31-16:25:06):
1. There are two ways, the first is to create a txt with FileStream, write data with StreamWriter, the period also to add judgment, whether the existence of this TXT file, if not exist on the creation, there is an append write. It's too much trouble!
2. The second type is direct File.appendalltext (string path, string contents), the first parameter is the TXT path + file name, and the second parameter is the write content. This method will determine the existence of the file itself, directly one step!
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Windows;namespaceppttojpg{ Public classMyLog { Public voidShowlog (stringlog) { //The first way, it's too much trouble.//StreamWriter SW = null; //if (! File.exists ("Log.txt"))//{ //FileStream fs = new FileStream ("Log.txt", FileMode.Create, FileAccess.Write); //SW = new StreamWriter (FS); //SW. WriteLine (log); // //remember to close it! Otherwise there's no word inside! //SW. Close (); //FS. Close (); //} //Else//{ //SW = File.appendtext ("Log.txt"); //SW. WriteLine (log); //SW. Close (); // //MessageBox.Show ("already has a log file!"); //} //the second method, relatively simple//\ r \ n to add to the front will not change line! File.appendalltext ("Log.txt","\ r \ n"+log); } }}
C # Learning Note (--c#) Create a text file txt and append write data