遍历删除文件夹及文件

1 public void DeleteFileTest(string Spath)
2 {
3 if (!Directory.Exists(Spath))
4 { return; }
5 string[] fPaths = Directory.GetFiles(Spath);
6 string[] fsPaths = Directory.GetDirectories(Spath);
7 try
8 {
9 for (int i = 0; i < fPaths.Length; i++)
10 {
11 MessageBox.Show(File.GetAttributes(fPaths[i]).ToString());
12 if (File.GetAttributes(fPaths[i]) == FileAttributes.Normal)
13 {
14 File.SetAttributes(fPaths[i], FileAttributes.Normal);
15 File.Delete(fPaths[i]);
16 }
17 else
18 File.Delete(fPaths[i]);
19 }
20 for (int d = 0; d < fsPaths.Length; d++)
21 {
22 DirectoryInfo Dinfo = new DirectoryInfo(fsPaths[d]);
23 if (Dinfo.Attributes != FileAttributes.Normal)
24 {
25 this.DeleteFileTest(fsPaths[d]); //递归
26   Dinfo.Attributes = FileAttributes.Normal;
27 Dinfo.Delete(true);
28 }
29 else
30 Dinfo.Delete(true);
31 }
32 }
33 catch
34 {
35 MessageBox.Show("Error");
36 return;
37 }
38
39 }
40
41  

 

posted @ 2009-12-30 09:12  槑槑  阅读(306)  评论(0编辑  收藏  举报