using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Microsoft.VisualBasic;using System.IO; namespace RubishClear{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region "全局的私有变量" //设置需要清理的文件夹共5个; public string[] _RubishPath=new string [5]; //设置用户名.; string _UserName=""; //设置筛选分隔符; char[] _split = new char[1] { ‘\\‘ }; //设置总文件大小的变量 long _AllMemory = 0; #endregion //获取文件夹的路径 void getRubishPath() { //获取当前的用户名.方法很多的.// Microsoft.VisualBasic.Devices.ServerComputer sc = new Microsoft.VisualBasic.Devices.ServerComputer(); string[] All = sc.FileSystem.SpecialDirectories.MyDocuments.Split(_split); _UserName = All[2]; _RubishPath.SetValue(@"C:\WINDOWS\SoftwareDistribution\Download\", 0); _RubishPath.SetValue(@"C:\WINDOWS\$hf_mig$\", 2); _RubishPath.SetValue(sc.FileSystem.SpecialDirectories.Temp + @"\", 3); // MessageBox.Show(sc.FileSystem.SpecialDirectories.Temp); _RubishPath.SetValue(@"C:\Documents and Settings\" + _UserName + @"\Recent\", 1); _RubishPath.SetValue(@"C:\WINDOWS\ie7updates\", 4); } //获取垃圾文件的名字&获取要删除的文件的大小./ void getFilename() { try { foreach (string path in _RubishPath) { // string[] arr = Microsoft.VisualBasic.FileIO.FileSystem.GetDirectories(path); foreach (string _FileName in Microsoft.VisualBasic.FileIO.FileSystem.GetFiles(path,Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories)) { try { FileInfo fi = new FileInfo(_FileName);//获取文件的所有信息. _AllMemory += fi.Length;//删除的文件的大小叠加起来. Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(_FileName);//删除文件.本人原来使用的是VB所以vb的东西搬了过来. //fi.Delete();//也可以使用fi.edlete事件. this.listBox1.Items.Add(_FileName);//添加删除的文件. } catch (Exception exp) { this.listBox2.Items.Add(exp.Message);//添加没有删除的文件. continue;//如果错误继续执行. } } } } catch { } } //获取&删除垃圾文件夹/ void getDic() { try { foreach (string path in _RubishPath) { foreach (string _PName in Microsoft.VisualBasic.FileIO.FileSystem.GetDirectories(path, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories)) { try { Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory (_PName,Microsoft.VisualBasic.FileIO.DeleteDirectoryOption.DeleteAllContents);//删除文件.本人原来使用的是VB所以vb的东西搬了过来. this.listBox1.Items.Add(_PName);//添加删除文件夹. } catch (Exception exp) { this.listBox2.Items.Add(exp.Message);//添加没有删除的文件夹. continue;//如果错误继续执行. } } } } catch { } } //登录事件 private void Form1_Load(object sender, EventArgs e) { getRubishPath(); } //chilk事件 private void button1_Click(object sender, EventArgs e) { getFilename();//开始清空文件. System.Threading.Thread.Sleep(100);//线程睡眠100毫秒. getDic();//开始清空文件夹. la1.Text = "程序共清空了:" + (int)(_AllMemory / 1024) + "K的垃圾文件"; MessageBox .Show ("已经成功删除了"+this.listBox1.Items.Count.ToString()+"项垃圾文件!"); } }}
时间: 2024-12-22 20:10:31