用C#清理文件






1.27/5 (9投票s)
2001年1月23日

91305

903
描述了使用 C# 进行文件访问和持久化
引言
本文档只是一个示例应用程序,它使用 .Net 框架删除指定目录中的不需要的文件。该示例使用System.IO
来管理任务,并使用递归函数来删除文件。
private void RemoveFiles(string strPath) { Directory dTemp = new Directory(strPath); for(int i = 0; i < lstExts.Items.Count; i++) { string s = lstExts.Items[i].ToString(); File[] fileList = dTemp.GetFiles(lstExts.Items[i].ToString()); for(int j = 0; j < fileList.Length; j++) { if(fileList[j].IsFile) { try { fileList[j].Delete(); } catch(SecurityException e) { lblStatus.Text = e.Message; } catch(Exception ex) { lblStatus.Text = ex.Message; } } } } } private void EmptyDirectory(string strPath) { if(!Directory.DirectoryExists(strPath)) return; Directory dirFinder = new Directory(strPath); Directory[] dirList = dirFinder.GetDirectories(); for(int i = 0; i < dirList.Length; i++) { string strTemp = strPath + "\\" + dirList[i].Name; EmptyDirectory(strTemp); RemoveFiles(strTemp); } RemoveFiles(strPath); }
这两个函数提供了递归删除文件的功能。本项目中使用的类
-
表单
-
文件
-
Directory(目录)
-
Button
-
ListBox
-
文本框