要想获取当前文佳夹下所有文件,可通过Directory类的Getfiles方法来实现,此方法返回字符串数组
格式为:
用for循环输出 class Program { static void Main(string[] args) { string path = @"E:\新建文件夹"; if (Directory.Exists(path)) { string[] dire = Directory.GetFiles(path); string[] file = Directory.GetDirectories(path); for (int i = 0; i < dire.Length; i++) { Console.WriteLine(dire[i]); } for (int i = 0; i < file.Length; i++) { Console.WriteLine(file[i]); } } else { Console.WriteLine("文件夹不存在"); } Console.Read(); } }
用foreach循环输出 class Program { static void Main(string[] args) { string path = @"E:\新建文件夹"; if (Directory.Exists(path)) { string[] dire = Directory.GetFiles(path); string[] file = Directory.GetDirectories(path); foreach (string item in dire) { Console.WriteLine(item); } foreach (string str in file) { Console.WriteLine(str); } } else { Console.WriteLine("文件夹不存在"); } Console.Read(); } }
原文地址:https://www.cnblogs.com/xiaowie/p/8669141.html
时间: 2024-11-05 22:04:22