zoukankan      html  css  js  c++  java
  • 文件操作

    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
  • 相关阅读:
    『C#基础』C#读写TXT文档
    『ExtJS』给 Panel Items 中的 Grid 更新数据
    『Spring.NET』常见错误整理(持续更新)
    『WPF』Timer的使用
    『WPF』使用 [Annotation] 注释来定制数据/实体类
    『WPF』DataGrid的使用
    vbs修改注册表
    利用C#重启远程计算机
    sql server2000创建表和修改表
    存储过程得到某个表的所有字段信息
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/7474485.html
Copyright © 2011-2022 走看看