温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#操作文本文件应用的示例分析

发布时间:2021-11-03 09:08:15 来源:亿速云 阅读:128 作者:柒染 栏目:编程语言

本篇文章给大家分享的是有关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 path3 = 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#操作文本文件应用的示例分析,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI