C#寫文件操作實例淺析
C#寫文件操作在我們編程過程中的簡單需求實現(xiàn),那么C#寫文件操作是如何實現(xiàn)的呢?C#寫文件操作的步驟以及需要注意的方面是什么呢?下面我們直接看看實現(xiàn)實例的內(nèi)容:
C#寫文件操作實例:
- using System.IO; //引用系統(tǒng)文件
- namespace tools
- {
- public class WriteFiles()
- {
- public class WiteFiles()
- {
- //構(gòu)造函數(shù)
- }
- public static void WriteLine(string format)
- {
- FileStream fs =
- new FileStream(Server.MapPath("log.txt"),
- FileMode.Append);
- StreamWriter streamWriter =
- new StreamWriter(fs);
- streamWriter.BaseStream.Seek(0, SeekOrigin.End);
- streamWriter.WriteLine(
- DateTime.Now.ToString(
- CultureInfo.CurrentCulture) + " " + format);
- streamWriter.Flush();
- }
- }
- }
C#寫文件操作之文件備注:
1 namespace 和class 可以不寫
2 直接在你想用寫文件的地方,在開始的using 中加上using System.IO
3 復制WriteLine方法即可
主要使用目的:
調(diào)試程序中使用,調(diào)用WriteLine(string format),輸出程序在執(zhí)行過程中想要看的變量內(nèi)容,根據(jù)log.txt的內(nèi)容能知道程序是否按照coder的意愿執(zhí)行。
C#寫文件操作的基本內(nèi)容就向你介紹到這里,希望對你了解和學習C#寫文件操作有所幫助。
【編輯推薦】