• 文件操作


    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
Copyright © 2020-2023  润新知