日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#操作文本文件應用實例簡析

C#操作文本文件應用實例:

站在用戶的角度思考問題,與客戶深入溝通,找到下花園網(wǎng)站設計與下花園網(wǎng)站推廣的解決方案,憑借多年的經驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站制作、成都外貿網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)絡空間、企業(yè)郵箱。業(yè)務覆蓋下花園地區(qū)。

 
 
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.IO;  
  11. using System.Text;  
  12.  
  13. /// ﹤summary﹥C#操作文本文件應用實例  
  14. /// C#操作文本文件的類  
  15. /// 程序(網(wǎng)站)所在目錄:D:\Test  
  16. /// 操作的文本文件:D:\Test\file  
  17. /// ﹤/summary﹥  
  18. public partial class _Default : System.Web.UI.Page  
  19. {  
  20. //在讀取txt文件中的中文時出現(xiàn)亂碼,  
  21. //解決辦法:StreamReader sr = new StreamReader(  
  22. fileName,Encoding.GetEncoding("gb2312"));  
  23. protected void Page_Load(object sender, EventArgs e)  
  24. {  
  25. #region C#讀取文本文件 (亂碼已解決)  
  26. {  
  27. string fileName = Server.MapPath(@"~\file") + @"\read.txt";  
  28. StreamReader sr = new StreamReader(fileName,   
  29. Encoding.GetEncoding("gb2312"));  
  30. //以gb2312字符編碼格式讀取文本。  
  31. string str;  
  32. string result = "";  
  33. while ((str = sr.ReadLine()) != null)//讀取每一行  
  34. {  
  35. result += str;  
  36. }  
  37. sr.Close();  
  38. sr.Dispose();  
  39. }  
  40. #endregion  
  41.  
  42. #region C#寫入文本文件C#操作文本文件應用實例  
  43. {  
  44. //string path = Server.MapPath(@".\file");  
  45. //這兩句等效。  
  46. //string path2 = Server.MapPath(@"~\file");  
  47. //CreateText():  
  48. //創(chuàng)建或打開一個文件用于寫入 UTF-8 編碼的文本。  
  49. StreamWriter rw = File.CreateText(Server.MapPath(@".\file")  
  50.  + @"\write.txt");  
  51. rw.WriteLine("你好"); //寫入三行數(shù)據(jù)。  
  52. rw.WriteLine("hello");  
  53. rw.WriteLine("中國");  
  54. rw.Flush();  
  55. rw.Close();  
  56. rw.Dispose();  
  57. }  
  58. #endregion  
  59.  
  60. #region 打開文本文件以進行讀取。(讀取中文出現(xiàn)亂碼)  
  61. {  //C#操作文本文件應用實例
  62. //OpenText():打開現(xiàn)有 UTF-8 編碼文本文件以進行讀取。  
  63. StreamReader sr = File.OpenText(  
  64. Server.MapPath(@".\file") + @"\open.txt");  
  65. StringBuilder output = new StringBuilder();  
  66. string str;  
  67. while ((str = sr.ReadLine()) != null)  
  68. {  
  69. output.Append(str + "+");  
  70. }  
  71. string result = output.ToString();  
  72. sr.Close();  
  73. sr.Dispose();  
  74. }  
  75. #endregion  
  76.  
  77. #region C#追加文本到現(xiàn)有文件  
  78. {  //C#操作文本文件應用實例
  79. //File.AppendText():  
  80. // 創(chuàng)建一個 StreamWriter,它將 UTF-8 編碼文本追加到現(xiàn)有文件。   
  81. StreamWriter sw = File.AppendText(  
  82. Server.MapPath(@".\file") + @"\append.txt");  
  83. sw.WriteLine("歡迎");  
  84. sw.WriteLine("來");  
  85. sw.WriteLine("中國");  
  86. sw.Flush();  
  87. sw.Close();  
  88. sw.Dispose();  
  89. }  
  90. #endregion  
  91.  
  92. #region C#拷貝文件  
  93. {  
  94. string from, to;  
  95. from = Server.MapPath(@".\file") + @"\copyFrom.txt";  
  96. to = Server.MapPath(@".\file") + @"\copyTo.txt";  
  97. File.Copy(from, to, true);  
  98. //true/false:是否允許改寫目標文件。如果目標文件不存在,會自動創(chuàng)建。  
  99. }  
  100. #endregion  
  101.  
  102. #region C#刪除文件  
  103. {  
  104. string delFile = Server.MapPath(@".\file") + @"\delFile.txt";  
  105. //要刪除的文件路徑  
  106. File.Delete(delFile);  
  107. }  
  108. #endregion  
  109.  
  110. #region C#移動文件  
  111. {  
  112. //string From, To;  
  113. //From = Server.MapPath(".") + @"\MoveFrom.txt";  
  114. //To = Server.MapPath(@".\file") + @"\MoveFromTo.txt";  
  115. //File.Move(From, To);//移動并可重明名  
  116. }  
  117. #endregion  
  118.  
  119. #region C#創(chuàng)建目錄 // Directory - DirectoryInfo  
  120. {  
  121. DirectoryInfo d = Directory.CreateDirectory(  
  122. Server.MapPath(@".\file") + @"\CreateDirectory");  
  123. //創(chuàng)建子目錄  
  124. DirectoryInfo d1 = d.CreateSubdirectory("CreateDirectory1");  
  125. DirectoryInfo d2 = d1.CreateSubdirectory("CreateDirectory2");  
  126.  
  127. //應用程序的當前工作目錄:  
  128. //D:\Program Files\Microsoft Visual Studio 8\Common7\IDE  
  129. string cur = Directory.GetCurrentDirectory();  
  130. //將當前目錄設為Server.MapPath(@".\file")  
  131. Directory.SetCurrentDirectory(Server.MapPath(@".\file"));  
  132. //(在當前工作目錄)創(chuàng)建目錄  
  133. DirectoryInfo d3 = Directory.CreateDirectory("sixAge2");  
  134. //創(chuàng)建目錄 C#操作文本文件應用實例
  135. DirectoryInfo d4 = Directory.CreateDirectory(@"sixAge2\sixAge2_1");  
  136. //應用程序的當前工作目錄  
  137. string cur1 = Directory.GetCurrentDirectory();  
  138. }  
  139. #endregion  
  140. }  

注釋:在D盤根目錄下創(chuàng)建以Test命明名的網(wǎng)站。。。

C#操作文本文件應用實例的基本內容就向你介紹到這里,希望對你了解和學習C#操作文本文件有所幫助。

【編輯推薦】

  1. C# 操作符之三元操作符淺析
  2. C# 操作符之 . 運算符應用詳解
  3. C# 操作符分類及應用淺析
  4. C#操作文本文件實例淺析
  5. C#操作文本文件之添加文本操作淺析

本文題目:C#操作文本文件應用實例簡析
文章出自:http://www.dlmjj.cn/article/djpigch.html