关于文件的读写

//写

string FilePath = Server.MapPath(Request.ApplicationPath)+"\\Error.Txt";//先获得该文件的实际地址
StreamWriter writer = null;

  if(!File.Exists(FilePath))// 检测有无文件
 {
        File.CreateText(FilePath);  //如果该文件不存在则建立
   }
   writer = File.AppendText(FilePath);//创建流
   writer.WriteLine(DateTime.Now +"@"+Error);//将文本写入
   writer.Close();//关闭

//读

//取得文件的实际路径
            string file_path = configpath + "\\counter.txt";

            if(!File.Exists(file_path))
            {
                File.CreateText(file_path);
            }

            //打开文件进行读取
            using(StreamReader srd = new StreamReader(file_path))
            {
                try
                {
                    while(srd.Peek() >= 0)//PEEK返回下一个可用字符但不使用,仅是判断
                    {
                        string str = srd.ReadLine();
                        count = UInt32.Parse(str);
                    }
                }
                catch(Exception )
                {
                    count = 0;
                }
            }
            object obj = count;
            Application["counter"] = obj;

原文地址:https://www.cnblogs.com/YepQX/p/1686016.html