栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#封装的常用文件操作类实例

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#封装的常用文件操作类实例

本文实例讲述了C#封装的常用文件操作类。分享给大家供大家参考。具体如下:

这个C#类封装了我们经常能用到的文件操作方法,包括读写文件、获取文件扩展名、复制文件、追加内容到文件、删除文件、移动文件、创建目录、递归删除文件及目录、列目录、列文件等,不可多得。

using System;
using System.Text;
using System.Web;
using System.IO;
namespace DotNet.Utilities
{
  public class FileOperate
  {
    #region 写文件
    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();
    }
    #endregion
    #region 读文件
    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;
    }
    #endregion
    #region 取得文件后缀名
    
    /// 
    /// 取后缀名
    /// 
    /// 文件名
    /// .gif|.html格式
    public static string GetPostfixStr(string filename)
    {
      int start = filename.LastIndexOf(".");
      int length = filename.Length;
      string postfix = filename.Substring(start, length - start);
      return postfix;
    }
    #endregion
    #region 写文件
    
    /// 
    /// 写文件
    /// 
    /// 文件路径
    /// 文件内容
    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 读文件
    
    /// 
    /// 读文件
    /// 
    /// 文件路径
    /// 
    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
    #region 追加文件
    
    /// 
    /// 追加文件
    /// 
    /// 文件路径
    /// 内容
    public static void FileAdd(string Path, string strings)
    {
      StreamWriter sw = File.AppendText(Path);
      sw.Write(strings);
      sw.Flush();
      sw.Close();
      sw.Dispose();
    }
    #endregion
    #region 拷贝文件
    
    /// 
    /// 拷贝文件
    /// 
    /// 原始文件
    /// 新文件路径
    public static void FileCoppy(string OrignFile, string NewFile)
    {
      File.Copy(OrignFile, NewFile, true);
    }
    #endregion
    #region 删除文件
    
    /// 
    /// 删除文件
    /// 
    /// 路径
    public static void FileDel(string Path)
    {
      File.Delete(Path);
    }
    #endregion
    #region 移动文件
    
    /// 
    /// 移动文件
    /// 
    /// 原始路径
    /// 新路径
    public static void FileMove(string OrignFile, string NewFile)
    {
      File.Move(OrignFile, NewFile);
    }
    #endregion
    #region 在当前目录下创建目录
    
    /// 
    /// 在当前目录下创建目录
    /// 
    /// 当前目录
    /// 新目录
    public static void FolderCreate(string OrignFolder, string NewFloder)
    {
      Directory.SetCurrentDirectory(OrignFolder);
      Directory.CreateDirectory(NewFloder);
    }
    /// 
    /// 创建文件夹
    /// 
    /// 
    public static void FolderCreate(string Path)
    {
      // 判断目标目录是否存在如果不存在则新建之
      if (!Directory.Exists(Path))
 Directory.CreateDirectory(Path);
    }
    #endregion
    #region 创建目录
    public static void FileCreate(string Path)
    {
      FileInfo CreateFile = new FileInfo(Path); //创建文件
      if (!CreateFile.Exists)
      {
 FileStream FS = CreateFile.Create();
 FS.Close();
      }
    }
    #endregion
    #region 递归删除文件夹目录及文件
    
    /// 
    /// 递归删除文件夹目录及文件
    /// 
    ///  
    /// 
    public static void DeleteFolder(string dir)
    {
      if (Directory.Exists(dir)) //如果存在这个文件夹删除之
      {
 foreach (string d in Directory.GetFileSystemEntries(dir))
 {
   if (File.Exists(d))
     File.Delete(d); //直接删除其中的文件     
   else
     DeleteFolder(d); //递归删除子文件夹
 }
 Directory.Delete(dir, true); //删除已空文件夹 
      }
    }
    #endregion
    #region 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
    
    /// 
    /// 指定文件夹下面的所有内容copy到目标文件夹下面
    /// 
    /// 原始路径
    /// 目标文件夹
    public static void CopyDir(string srcPath, string aimPath)
    {
      try
      {
 // 检查目标目录是否以目录分割字符结束如果不是则添加之
 if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
   aimPath += Path.DirectorySeparatorChar;
 // 判断目标目录是否存在如果不存在则新建之
 if (!Directory.Exists(aimPath))
   Directory.CreateDirectory(aimPath);
 // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
 //如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
 //string[] fileList = Directory.GetFiles(srcPath);
 string[] fileList = Directory.GetFileSystemEntries(srcPath);
 //遍历所有的文件和目录
 foreach (string file in fileList)
 {
   //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
   if (Directory.Exists(file))
     CopyDir(file, aimPath + Path.GetFileName(file));
   //否则直接Copy文件
   else
     File.Copy(file, aimPath + Path.GetFileName(file), true);
 }
      }
      catch (Exception ee)
      {
 throw new Exception(ee.ToString());
      }
    }
    #endregion
    #region 获取指定文件夹下所有子目录及文件(树形)
    
    /// 
    /// 获取指定文件夹下所有子目录及文件
    /// 
    /// 详细路径
    public static string GetFoldAll(string Path)
    {
      string str = "";
      DirectoryInfo thisOne = new DirectoryInfo(Path);
      str = ListTreeShow(thisOne, 0, str);
      return str;
    }
    /// 
    /// 获取指定文件夹下所有子目录及文件函数
    /// 
    /// 指定目录
    /// 默认起始值,调用时,一般为0
    /// 用于迭加的传入值,一般为空
    /// 
    public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//递归目录 文件
    {
      DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录
      foreach (DirectoryInfo dirinfo in subDirectories)
      {
 if (nLevel == 0)
 {
   Rn += "├";
 }
 else
 {
   string _s = "";
   for (int i = 1; i <= nLevel; i++)
   {
     _s += "│ ";
   }
   Rn += _s + "├";
 }
 Rn += "" + dirinfo.Name.ToString() + "
"; FileInfo[] fileInfo = dirinfo.GetFiles(); //目录下的文件 foreach (FileInfo fInfo in fileInfo) { if (nLevel == 0) { Rn += "│ ├"; } else { string _f = ""; for (int i = 1; i <= nLevel; i++) { _f += "│ "; } Rn += _f + "│ ├"; } Rn += fInfo.Name.ToString() + "
"; } Rn = ListTreeShow(dirinfo, nLevel + 1, Rn); } return Rn; } /// /// 获取指定文件夹下所有子目录及文件(下拉框形) /// /// 详细路径 ///下拉列表名称 ///默认选择模板名称 public static string GetFoldAll(string Path, string DropName, string tplPath) { string strDrop = ""; } /// /// 获取指定文件夹下所有子目录及文件函数 /// /// 指定目录 /// 默认起始值,调用时,一般为0 /// 用于迭加的传入值,一般为空 /// 默认选择模板名称 /// public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn, string tplPath)//递归目录 文件 { DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录 foreach (DirectoryInfo dirinfo in subDirectories) { Rn += "

希望本文所述对大家的C#程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/125647.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号