DirectoryInfo遍历文件及文件夹

DirectoryInfo遍历文件及文件夹

1.GetDirectories()方法遍历文件夹

string path = Application.StartupPath;
DirectoryInfo myDirectoryInfo = new DirectoryInfo(path);
DirectoryInfo[] myDirectoryInfoArray = myDirectoryInfo.GetDirectories();
foreach (var item in myDirectoryInfoArray)
{
}

2.GetFiles()方法遍历文件

string path = Application.StartupPath;
DirectoryInfo myDirectoryInfo = new DirectoryInfo(path);
FileInfo[] myFileInfoArray = myDirectoryInfo.GetFiles();
foreach (var item in myFileInfoArray)
{
}

3.GetFileSystemInfo()遍历文件和文件夹

string path = Application.StartupPath;
DirectoryInfo myDirectoryInfo = new DirectoryInfo(path);
FileSystemInfo[] myFileSystemInfoArray = myDirectoryInfo.GetFileSystemInfos();
foreach (var item in myFileSystemInfoArray)
{
}