首页 > Web开发 > 详细

mvc 读写txt文档

时间:2018-04-03 15:12:55      阅读:182      评论:0      收藏:0      [点我收藏+]

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

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文档

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!