C#操作文本文件应用实例简析

开发 后端
C#操作文本文件应用实例主要向你介绍了具体的C#操作文本文件的应用实现,C#操作文本文件需要注意什么呢?那么本文就向你介绍相关的内容。
C#操作文本文件应用实例:
  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. /// 程序(网站)所在目录:D:\Test  
  16. /// 操作的文本文件:D:\Test\file  
  17. /// ﹤/summary﹥  
  18. public partial class _Default : System.Web.UI.Page  
  19. {  
  20. //在读取txt文件中的中文时出现乱码,  
  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. //创建或打开一个文件用于写入 UTF-8 编码的文本。  
  49. StreamWriter rw = File.CreateText(Server.MapPath(@".\file")  
  50.  + @"\write.txt");  
  51. rw.WriteLine("你好"); //写入三行数据。  
  52. rw.WriteLine("hello");  
  53. rw.WriteLine("中国");  
  54. rw.Flush();  
  55. rw.Close();  
  56. rw.Dispose();  
  57. }  
  58. #endregion  
  59.  
  60. #region 打开文本文件以进行读取。(读取中文出现乱码)  
  61. {  //C#操作文本文件应用实例
  62. //OpenText():打开现有 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#追加文本到现有文件  
  78. {  //C#操作文本文件应用实例
  79. //File.AppendText():  
  80. // 创建一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件。   
  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:是否允许改写目标文件。如果目标文件不存在,会自动创建。  
  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#创建目录 // Directory - DirectoryInfo  
  120. {  
  121. DirectoryInfo d = Directory.CreateDirectory(  
  122. Server.MapPath(@".\file") + @"\CreateDirectory");  
  123. //创建子目录  
  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. //(在当前工作目录)创建目录  
  133. DirectoryInfo d3 = Directory.CreateDirectory("sixAge2");  
  134. //创建目录 C#操作文本文件应用实例
  135. DirectoryInfo d4 = Directory.CreateDirectory(@"sixAge2\sixAge2_1");  
  136. //应用程序的当前工作目录  
  137. string cur1 = Directory.GetCurrentDirectory();  
  138. }  
  139. #endregion  
  140. }  

注释:在D盘根目录下创建以Test命明名的网站。。。

C#操作文本文件应用实例的基本内容就向你介绍到这里,希望对你了解和学习C#操作文本文件有所帮助。

【编辑推荐】

  1. C# 操作符之三元操作符浅析
  2. C# 操作符之 . 运算符应用详解
  3. C# 操作符分类及应用浅析
  4. C#操作文本文件实例浅析
  5. C#操作文本文件之添加文本操作浅析
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-08-19 17:44:15

C#操作文本文件

2009-08-20 10:17:27

C#操作文本文件

2009-08-20 09:15:20

C#操作文本文件

2009-08-20 09:26:14

C#操作文本文件

2009-08-26 11:53:56

C#打印文本文件

2009-09-02 19:13:08

C#处理文本文件

2009-08-06 18:33:45

C#处理文本文件

2010-01-11 17:05:32

VB.NET操作文本文

2009-09-02 19:08:03

C#实现读取文本文件

2009-08-12 17:59:48

C#读取文本文

2009-09-04 15:56:35

写入文本文件

2009-09-03 10:52:41

C#递归树

2009-08-12 17:42:57

C#读文本文件

2010-02-01 14:26:50

C++读写文本文件

2009-08-13 14:36:40

C#结构体构造函数

2009-08-12 16:38:35

C#读取XML节点

2010-04-30 17:38:31

Unix文本

2021-11-29 09:46:11

FileReaderJava开发

2009-08-13 15:48:57

C#指针

2009-10-29 14:16:32

VB.NET读写文本文
点赞
收藏

51CTO技术栈公众号