C# DirectoryInfo读取文件夹当前目录或所有子目录

string filepath = "D:\\balabala\\balabala";
DirectoryInfo folder = new DirectoryInfo(filepath);
//遍历文件夹中的Excel
foreach (FileInfo file in folder.GetFiles("*.xlsx", SearchOption.TopDirectoryOnly))
{
...
}
  1. SearchOption.TopDirectoryOnly
    仅包含当前目录
  2. SearchOption.AllDirectories
    所有子目录:会读取文件夹中子文件夹的文件,也就是会读取所有的文件,子文件夹的子文件夹也会读取噢

详解:
(看了上面的内容应该就会用了,不想看啰嗦的人不用继续往下看)

  1. GetFiles()
    官方解释:
// 摘要:
//     Returns a file list from the current directory.
//     从当前目录返回文件列表. 
// 返回结果:
//     An array of type System.IO.FileInfo.
//	   类型为System.IO.FileInfo的数组.
// 异常:
//   T:System.IO.DirectoryNotFoundException:
//     The path is invalid, such as being on an unmapped drive.
//     路径无效.
public FileInfo[] GetFiles();
  1. GetFiles(string searchPattern, SearchOption searchOption);
    官方解释:
// 摘要:
//     Returns a file list from the current directory matching the given search pattern
//     and using a value to determine whether to search subdirectories.
//     从当前目录返回与给定搜索模式匹配的文件列表
//     并使用一个值来确定是否要搜索子目录.
// 参数:
//   searchPattern:
//     The search string to match against the names of files. This parameter can contain
//     a combination of valid literal path and wildcard (* and ?) characters (see Remarks),
//     but doesn't support regular expressions. The default pattern is "*", which returns
//     all files.
//	   要匹配文件名称的搜索字符串。此参数可以包含有效的字面值路径和通配符(*和?)字符的组合,
//	   但不支持正则表达式。默认模式是"*",它返回所有文件。
//   searchOption:
//     One of the enumeration values that specifies whether the search operation should
//     include only the current directory or all subdirectories.
//		枚举值之一,指定搜索操作是否应该仅包含当前目录或所有子目录.
public FileInfo[] GetFiles(string searchPattern, SearchOption searchOption);
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值