C # Learning Note (--c#) Create a text file txt and append write data

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.