0%

CSharp删除文件或文件夹

前面啥也没有

删除文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public void DeleteFiles(string path)
{
try
{
string[] files = Directory.GetFiles(path);
List<string> picFiles = new List<string>();
foreach (string f in files)
{
string fileName = Path.GetFileName(f);
if (fileName.ToUpper().Contains("LW"))
picFiles.Add(f);
}
picFiles.ForEach((f) =>
{
FileInfo fi = new FileInfo(f);
DateTime dtWrite = fi.LastWriteTime;
if ((DateTime.Now - dtWrite).Days > deleteDays)
fi.Delete();
});
}
catch (System.Exception ex)
{

}
}

删除文件夹

1
2
3
4
5
6
7
string[] directories = Directory.GetDirectories(path);
directories.ToList().ForEach((dir) =>
{
if ((DateTime.Now - Directory.GetLastWriteTime(dir)).Days > deleteDays)
Directory.Delete(dir, true);
});