DirectoryInfo dirInfo = new DirectoryInfo(path); //類型名不區分大小寫 FileInfo[] files = dirInfo.GetFiles("*.pdf", SearchOption.AllDirectories); foreach (FileInfo fileInfo in files) { Console.WriteLine(fileInfo.FullName); }
2、遍歷文件夾下所有文件。
DirectoryInfo dirInfo = new DirectoryInfo(path); FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories); foreach (FileInfo fileInfo in files) { string filename = fileInfo.Name.ToLower(); //可在這裡篩選多種文件類型 if (filename.EndsWith(".jpg") || filename.EndsWith(".png")) { Console.WriteLine(fileInfo.FullName); } }
3、判斷目錄是否存在。
if (!Directory.Exists(path)) { Console.WriteLine(path + " not exist"); }
4、創建目錄。 Directory.CreateDirectory(path); //可創建多級目錄