资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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.Collections;  using System.IO;  using System.Text;   /// ///C#读取文件内容另存 /// Summary description for ReadFile  ///  public class ReadFile  {   public ReadFile()   {     //  C#读取文件内容   // TODO: Add constructor logic here   //      }        public int[,] ReadFileToArray()      {   int[,] iret = null;  //C#读取文件内容另存  ArrayList alNumLine = getFileContent();     string[] strLineArr = null;   if (alNumLine.Count > 0)          {      strLineArr = Convert.ToString(alNumLine[0]).Trim(',').Split(',');    iret = new int[alNumLine.Count, strLineArr.Length];      for (int i = 0; i < alNumLine.Count; i++)              {   strLineArr = Convert.ToString(alNumLine[i]).Trim(',').Split(',');   for (int j = 0; j < strLineArr.Length; j++)   {      iret[i, j] = Convert.ToInt32(strLineArr[j]);     }              }          }           return iret;       }   //C#读取文件内容另存     public ArrayList getFileContent()      {    ArrayList alRet = new ArrayList();    string strFilePath = HttpContext.Current.Server.MapPath("~") + "/array.txt";    if (!File.Exists(strFilePath))          {   HttpContext.Current.Response.Write("文件[" + strFilePath + "]不存在。");              return alRet;          }   //C#读取文件内容另存        try         {   //读出一行文本,并临时存放在ArrayList中      StreamReader sr = new StreamReader(strFilePath,  Encoding.GetEncoding("gb2312"));    string l;     while ((l = sr.ReadLine()) != null)              {     if (!string.IsNullOrEmpty(l.Trim()))          alRet.Add(l.Trim());              }              sr.Close();          }          catch (IOException ex)          {    HttpContext.Current.Response.Write("读文件出错!请检查文件是否正确。");    HttpContext.Current.Response.Write(ex.ToString());          }           return alRet;   //C#读取文件内容另存     }  }

关于C#中怎么读取文件内容并实现另存就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站栏目:C#中怎么读取文件内容并实现另存
分享地址:http://cdkjz.cn/article/gdejpd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220