日期:2014-05-18 浏览次数:21174 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; using System.Text.RegularExpressions; namespace MyDelete { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Thread thread;//构造线程 List<string> msgList = new List<string>(); private void StartBtn_Click(object sender, EventArgs e) { if(this.FilePath.Text==null || this.FilePath.Text==string.Empty) { MessageBox.Show("文件路径不能为空","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } Regex reg = new Regex(@"[url=file://\\[^\\]*$]\\[^\\]*$[/url]", RegexOptions.IgnoreCase); if(!reg.IsMatch(this.FilePath.Text)) { MessageBox.Show("请输入正确的文件路径", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } thread = new Thread(new ParameterizedThreadStart(DeleteFiles));//委托线程调用带参数的方法 thread.IsBackground = true; thread.Start(this.FilePath.Text); //执行删除方法 } delegate void InvokeText(string str); public void DeleteFiles(object state)//定义一个删除文件的方法 { string filenameslog = "";//用来保存删除记录 int deletefiles = 0; string strDir = state as string; while (true) { if (Directory.Exists(strDir))//判断指定的目录是否存在 { string[] strFiles = Directory.GetFiles(strDir);//获取该目录下的所有文件 if (strFiles.Length > 0)//判断文件夹下是否有文件 { filenameslog = (++deletefiles) + ":" + "删除成功,时间:" + DateTime.Now.ToString() + "\r\n"; InvokeText txt = delegate(string text) { msgList.Add(text); if (msgList.Count > 10) { msgList.RemoveAt(0); } string msgs = ""; foreach (string s in msgList) { msgs += s; } this.DeleteLog.Text = msgs; }; this.DeleteLog.Invoke(txt, new object[1] { filenameslog }); foreach (string strFile in strFiles) { File.Delete(strFile); } } } Thread.Sleep(1500); } } private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) this.Visible = false; else this.Visible = true; } private void notifyIcon1_DoubleClick(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Visible = true; this.WindowState = FormWindowState.Normal; } else this.Visible = true; } private void ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void Form1_Load(ob