1、Directory 操作文件夹
1)创建文件夹
1 Directory.CreateDirectory(@"C:\a"); 2 Console.WriteLine("创建成功"); 3 Console.ReadKey();
2)删除文件夹
1 Directory.Delete(@"C:\a", true); 2 Console.WriteLine("删除成功"); 3 Console.ReadKey()
3)剪切文件夹
1 Directory.Move(@"c:\a", @"C:\Users\SpringRain\Desktop\new"); 2 Console.WriteLine("剪切成功"); 3 Console.ReadKey();
4)获得指定目录(文件夹)下所有文件(不包括文件夹)的全路径
1 string[] path = Directory.GetFiles(@"C:\Users\SpringRain\Desktop\Picture", "*.jpg"); 2 for (int i = 0; i < path.Length; i++) 3 { 4 Console.WriteLine(path[i]); 5 } 6 Console.ReadKey();
5)获得指定目录下所有文件夹的全路径
1 string[] path = Directory.GetDirectories(@"C:\Users\SpringRain\Desktop\new"); 2 for (int i = 0; i < path.Length; i++) 3 { 4 Console.WriteLine(path[i]); 5 } 6 Console.ReadKey();
6)判断指定的文件夹是否存在
1 if (Directory.Exists(@"C:\a\b")) 2 { 3 for (int i = 0; i < 100; i++) 4 { 5 Directory.CreateDirectory(@"C:\a\b\" + i); 6 } 7 } 8 Console.WriteLine("OK"); 9 Console.ReadKey();
时间: 2024-10-05 05:50:32