C#操作文本文件應用實例簡析
作者:dongxiaohui2008
C#操作文本文件應用實例主要向你介紹了具體的C#操作文本文件的應用實現,C#操作文本文件需要注意什么呢?那么本文就向你介紹相關的內容。
C#操作文本文件應用實例:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.IO;
- using System.Text;
- /// ﹤summary﹥C#操作文本文件應用實例
- /// C#操作文本文件的類
- /// 程序(網站)所在目錄:D:\Test
- /// 操作的文本文件:D:\Test\file
- /// ﹤/summary﹥
- public partial class _Default : System.Web.UI.Page
- {
- //在讀取txt文件中的中文時出現亂碼,
- //解決辦法:StreamReader sr = new StreamReader(
- fileName,Encoding.GetEncoding("gb2312"));
- protected void Page_Load(object sender, EventArgs e)
- {
- #region C#讀取文本文件 (亂碼已解決)
- {
- string fileName = Server.MapPath(@"~\file") + @"\read.txt";
- StreamReader sr = new StreamReader(fileName,
- Encoding.GetEncoding("gb2312"));
- //以gb2312字符編碼格式讀取文本。
- string str;
- string result = "";
- while ((str = sr.ReadLine()) != null)//讀取每一行
- {
- result += str;
- }
- sr.Close();
- sr.Dispose();
- }
- #endregion
- #region C#寫入文本文件C#操作文本文件應用實例
- {
- //string path = Server.MapPath(@".\file");
- //這兩句等效。
- //string path2 = Server.MapPath(@"~\file");
- //CreateText():
- //創建或打開一個文件用于寫入 UTF-8 編碼的文本。
- StreamWriter rw = File.CreateText(Server.MapPath(@".\file")
- + @"\write.txt");
- rw.WriteLine("你好"); //寫入三行數據。
- rw.WriteLine("hello");
- rw.WriteLine("中國");
- rw.Flush();
- rw.Close();
- rw.Dispose();
- }
- #endregion
- #region 打開文本文件以進行讀取。(讀取中文出現亂碼)
- { //C#操作文本文件應用實例
- //OpenText():打開現有 UTF-8 編碼文本文件以進行讀取。
- StreamReader sr = File.OpenText(
- Server.MapPath(@".\file") + @"\open.txt");
- StringBuilder output = new StringBuilder();
- string str;
- while ((str = sr.ReadLine()) != null)
- {
- output.Append(str + "+");
- }
- string result = output.ToString();
- sr.Close();
- sr.Dispose();
- }
- #endregion
- #region C#追加文本到現有文件
- { //C#操作文本文件應用實例
- //File.AppendText():
- // 創建一個 StreamWriter,它將 UTF-8 編碼文本追加到現有文件。
- StreamWriter sw = File.AppendText(
- Server.MapPath(@".\file") + @"\append.txt");
- sw.WriteLine("歡迎");
- sw.WriteLine("來");
- sw.WriteLine("中國");
- sw.Flush();
- sw.Close();
- sw.Dispose();
- }
- #endregion
- #region C#拷貝文件
- {
- string from, to;
- from = Server.MapPath(@".\file") + @"\copyFrom.txt";
- to = Server.MapPath(@".\file") + @"\copyTo.txt";
- File.Copy(from, to, true);
- //true/false:是否允許改寫目標文件。如果目標文件不存在,會自動創建。
- }
- #endregion
- #region C#刪除文件
- {
- string delFile = Server.MapPath(@".\file") + @"\delFile.txt";
- //要刪除的文件路徑
- File.Delete(delFile);
- }
- #endregion
- #region C#移動文件
- {
- //string From, To;
- //From = Server.MapPath(".") + @"\MoveFrom.txt";
- //To = Server.MapPath(@".\file") + @"\MoveFromTo.txt";
- //File.Move(From, To);//移動并可重明名
- }
- #endregion
- #region C#創建目錄 // Directory - DirectoryInfo
- {
- DirectoryInfo d = Directory.CreateDirectory(
- Server.MapPath(@".\file") + @"\CreateDirectory");
- //創建子目錄
- DirectoryInfo d1 = d.CreateSubdirectory("CreateDirectory1");
- DirectoryInfo d2 = d1.CreateSubdirectory("CreateDirectory2");
- //應用程序的當前工作目錄:
- //D:\Program Files\Microsoft Visual Studio 8\Common7\IDE
- string cur = Directory.GetCurrentDirectory();
- //將當前目錄設為Server.MapPath(@".\file")
- Directory.SetCurrentDirectory(Server.MapPath(@".\file"));
- //(在當前工作目錄)創建目錄
- DirectoryInfo d3 = Directory.CreateDirectory("sixAge2");
- //創建目錄 C#操作文本文件應用實例
- DirectoryInfo d4 = Directory.CreateDirectory(@"sixAge2\sixAge2_1");
- //應用程序的當前工作目錄
- string cur1 = Directory.GetCurrentDirectory();
- }
- #endregion
- }
- }
注釋:在D盤根目錄下創建以Test命明名的網站。。。
C#操作文本文件應用實例的基本內容就向你介紹到這里,希望對你了解和學習C#操作文本文件有所幫助。
【編輯推薦】
責任編輯:仲衡
來源:
CSDN博客