Directory文件类的实例讲解
时间:2020-12-12 09:51:54|栏目:.NET代码|点击: 次
创建一个新文件
Directory.CreateDirectory(@"C: \Users\enle\Desktop\new");//路径 Console.WriteLine("创建成功"); Console.ReadKey()
删除一个文件
Directory.Delete(@"C: \Users\enle\Desktop\new", true); Console.WriteLine("删除成功"); Console.ReadKey()
剪切一个文件
Directory.Move(@"C:\Users\enle\Desktop\new", @"C:\Users\enle\Desktop\new1"); Console.Write("剪切成功"); Console.ReadKey()
读取文件所有文件的全路径
string[] path = Directory.GetFiles(@"C:\Users\enle\Desktop\2", "*.pdf");//"*.pdf" 对读取文件的格式,进行限定 for (int i = 0; i < path.Length; i++) { Console.WriteLine(path[i]); }
文件夹的路径
string[] path = Directory.GetDirectories(@"C:\Users\enle\Desktop\2"); for (int i = 0; i < path.Length; i++) { Console.WriteLine(path[i]); } Console.ReadKey()
创建100个文件夹
if (Directory .Exists (@"C:\Users\enle\Desktop\new1")) { for (int i = 0; i < 100; i++) { Directory.CreateDirectory(@"C: \Users\enle\Desktop\new1\" + i); } Console.WriteLine("OK"); Console.ReadKey();