文件操作

protected void Write_Txt(string FileName, string Content)
       {
           Encoding code = Encoding.GetEncoding("gb2312");
           string htmlfilename = HttpContext.Current.Server.MapPath("Precious\" + FileName + ".txt"); //保存文件的路径
           string str = Content;
           StreamWriter sw = null;
           {
               try
               {
                   sw = new StreamWriter(htmlfilename, false, code);
                   sw.Write(str);
                   sw.Flush();
               }
               catch { }
           }
           sw.Close();
           sw.Dispose();
 
       }
protected string Read_Txt(string filename)
        {
 
            Encoding code = Encoding.GetEncoding("gb2312");
            string temp = HttpContext.Current.Server.MapPath("Precious\" + filename + ".txt");
            string str = "";
            if (File.Exists(temp))
            {
                StreamReader sr = null;
                try
                {
                    sr = new StreamReader(temp, code);
                    str = sr.ReadToEnd(); // 读取文件
                }
                catch { }
                sr.Close();
                sr.Dispose();
            }
            else
            {
                str = "";
            }
            return str;
        }
#region 写文件
      /****************************************
       * 函数名称:WriteFile
       * 功能说明:当文件不存时,则创建文件,并追加文件
       * 参    数:Path:文件路径,Strings:文本内容
       * 调用示列:
       *           string Path = Server.MapPath("Default2.aspx");       
       *           string Strings = "这是我写的内容啊";
       *           DotNet.Utilities.FileOperate.WriteFile(Path,Strings);
      *****************************************/
      /// <summary>
      /// 写文件
      /// </summary>
      /// <param name="Path">文件路径</param>
      /// <param name="Strings">文件内容</param>
      public static void WriteFile(string Path, string Strings)
      {
 
          if (!System.IO.File.Exists(Path))
          {
              System.IO.FileStream f = System.IO.File.Create(Path);
              f.Close();
              f.Dispose();
          }
          System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
          f2.WriteLine(Strings);
          f2.Close();
          f2.Dispose();
 
 
      }
      #endregion
 
      #region 读文件
      /****************************************
       * 函数名称:ReadFile
       * 功能说明:读取文本内容
       * 参    数:Path:文件路径
       * 调用示列:
       *           string Path = Server.MapPath("Default2.aspx");       
       *           string s = DotNet.Utilities.FileOperate.ReadFile(Path);
      *****************************************/
      /// <summary>
      /// 读文件
      /// </summary>
      /// <param name="Path">文件路径</param>
      /// <returns></returns>
      public static string ReadFile(string Path)
      {
          string s = "";
          if (!System.IO.File.Exists(Path))
              s = "不存在相应的目录";
          else
          {
              StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
              s = f2.ReadToEnd();
              f2.Close();
              f2.Dispose();
          }
 
          return s;
      }
      #endregion
原文地址:https://www.cnblogs.com/vaevvaev/p/7474485.html