C#实现文件和目录操作大全

最完整的文件和目录操作

资料来源不易。。。。。

 

 1. 检测指定目录是否存在

public static class DirFile
 {
  /// <summary>
  /// 检测指定目录是否存在
  /// </summary>
  /// <param name="directoryPath">目录的绝对路径</param>
  /// <returns></returns>
  public static bool IsExistDirectory(string directoryPath)
  {
   return Directory.Exists(directoryPath);
  }

 

  2. 获取指定目录中所有文件列表

  /// <summary>
  /// 获取指定目录中所有文件列表
  /// </summary>
  /// <param name="directoryPath">指定目录的绝对路径</param>
  public static string[] GetFileNames(string directoryPath)
  {
   //如果目录不存在,则抛出异常
   if (!IsExistDirectory(directoryPath))
   {
    throw new FileNotFoundException();
   }
   //获取文件列表
   return Directory.GetFiles(directoryPath);
  }

 

 3. 获取指定目录中所有子目录列表

  /// <summary>
  /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,使用重载方法
  /// </summary>
  /// <param name="directoryPath">指定目录的绝对路径</param>
  public static string[] GetDirectories(string directoryPath)
  {
   try
   {
    return Directory.GetDirectories(directoryPath);
   }
   catch (IOException ex)
   {
    throw ex;
   }
  }

 

 4. 获取指定目录及子目录中所有文件列表

  /// <summary>
  /// 获取指定目录及子目录中所有文件列表
  /// </summary>
  /// <param name="directoryPath">指定目录的绝对路径</param>
  public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
  {
   //如果目录不存在,则抛出异常
   if (!IsExistDirectory(directoryPath))
   {
    throw new FileNotFoundException();
   }
   try
   {
    if (isSearchChild)
    {
     return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
    }
    else
    {
     return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
    }
   }
   catch (IOException ex)
   {
    throw ex;
   }
  }

 5. 检测指定目录是否为空

  /// <summary>
  /// 检测指定目录是否为空
  /// </summary>
  /// <param name="directoryPath">指定目录的绝对路径</param>
  public static bool IsEmptyDirectory(string directoryPath)
  {
   try
   {
    //判断是否存在文件
    string[] fileNames = GetFileNames(directoryPath);
    if (fileNames.Length > 0)
    {
     return false;
    }
    //判断是否存在文件夹
    string[] directoryNames = GetDirectories(directoryPath);
    if (directoryNames.Length > 0)
    {
     return false;
    }
    return true;
   }
   catch
   {
    return true;
   }
  }

 6. 检测指定目录中是否存在指定的文件

  /// <summary>
  /// 检测指定目录中是否存在指定的文件
  /// </summary>
  /// <param name="directoryPath">指定目录的绝对路径</param>
  public static bool Contains(string directoryPath, string searchPattern)
  {
   try
   {
    //获取指定的文件列表
    string[] fileNames = GetFileNames(directoryPath, searchPattern, false);
 
    //判断指定文件是否存在
    if (fileNames.Length == 0)
    {
     return false;
    }
    else
    {
     return true;
    }
   }
   catch (Exception ex)
   {
    throw new Exception(ex.Message);
   }
  }

 7. 创建目录

<
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值