码迷,mamicode.com
首页 > Web开发 > 详细

mvc 读写txt文档

时间:2018-04-03 15:20:50      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:txt   empty   pos   判断   文件   pat   file   list   读写   

-----------------写入内容----------------

string userfile = "UserData.txt";
StreamWriter sw = null;

//判断是否存在
if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(@"../" + userfile)))
{

  //不存在,新建
  System.IO.File.Create(System.Web.HttpContext.Current.Server.MapPath(@"../" + userfile)).Close();
  sw = new StreamWriter(Server.MapPath(@"../" + userfile), true, System.Text.Encoding.Default);

  sw.Write("用户名");
  sw.Write("," + "手机号");
  sw.Write("," + "地址");
  sw.WriteLine();
}
else
{
  sw = new StreamWriter(Server.MapPath(@"../" + userfile), true, System.Text.Encoding.Default);
}

  sw.Write(“想要写入的内容”);
  sw.WriteLine();
  sw.Close(); //关闭流

 

-----------------读取内容----------------

string uListPath = @"../" + "UserData.txt";

string content = string.Empty;
if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(uListPath)))
{
  //文件不存在
}
else
{
  using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(uListPath),   System.Text.Encoding.Default))
  {
    content = sr.ReadToEnd(); // 读取txt文件内容
  }
}

 

mvc 读写txt文档

标签:txt   empty   pos   判断   文件   pat   file   list   读写   

原文地址:https://www.cnblogs.com/yao3364/p/8708550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!