string filepath = "D:\\balabala\\balabala";
DirectoryInfo folder = new DirectoryInfo(filepath);
//遍历文件夹中的Excel
foreach (FileInfo file in folder.GetFiles("*.xlsx", SearchOption.TopDirectoryOnly))
{
...
}
- SearchOption.TopDirectoryOnly
仅包含当前目录 - SearchOption.AllDirectories
所有子目录:会读取文件夹中子文件夹的文件,也就是会读取所有的文件,子文件夹的子文件夹也会读取噢
详解:
(看了上面的内容应该就会用了,不想看啰嗦的人不用继续往下看)
- 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();
- 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);