基本信息
源码名称:C# 清理回收站
源码大小:0.03M
文件格式:.zip
开发语言:C#
更新时间:2015-10-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace Recycle
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TreeView treeView1;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.label1 = new System.Windows.Forms.Label();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.treeView1 = new System.Windows.Forms.TreeView();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "选择驱动器:";
			// 
			// comboBox1
			// 
			this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox1.Location = new System.Drawing.Point(96, 8);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(240, 20);
			this.comboBox1.TabIndex = 1;
			this.comboBox1.SelectedIndexChanged  = new System.EventHandler(this.comboBox1_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(264, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "显示指定驱动器回收站里的内容:";
			// 
			// treeView1
			// 
			this.treeView1.ImageList = this.imageList1;
			this.treeView1.Location = new System.Drawing.Point(8, 56);
			this.treeView1.Name = "treeView1";
			this.treeView1.Size = new System.Drawing.Size(328, 152);
			this.treeView1.TabIndex = 3;
			this.treeView1.BeforeExpand  = new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(16, 216);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(144, 23);
			this.button1.TabIndex = 4;
			this.button1.Text = "清空回收站指定内容";
			this.button1.Click  = new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(184, 216);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(144, 23);
			this.button2.TabIndex = 5;
			this.button2.Text = "清空回收站所有内容";
			this.button2.Click  = new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(344, 246);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.treeView1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.comboBox1);
			this.Controls.Add(this.label1);
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "演示操作回收站";
			this.Load  = new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{//初始化逻辑驱动器组合框中的列表项
			string []MyDrivers=Directory.GetLogicalDrives();
			foreach(string MyDriver in MyDrivers)
				this.comboBox1.Items.Add(MyDriver);
			this.comboBox1.Text="C:\\";
		}
		private void MyAddTreeNodeFunction(TreeNode node,string MyPath)
		{//增加树节点
			string[] MyDirs=null;
			string[] MyFiles=null;
			try
			{
				MyDirs=Directory.GetDirectories(MyPath);
			}
			catch(Exception Err)
			{
				MessageBox.Show("读取文件夹信息发生错误!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);			
				return;
			}
			for (int j=0;j<MyDirs.Length;j  )
			{
				//添加新的子节点
				TreeNode MyNewNode= new TreeNode(MyDirs[j].ToString().Substring(MyDirs[j].ToString().LastIndexOf("\\") 1));
				//设置不选定状态下的图标
				MyNewNode.ImageIndex =2;
				//设置打开状态下的图标
				MyNewNode.SelectedImageIndex =0;
				node.Nodes.Add(MyNewNode);
			}
			try
			{
				MyFiles=Directory.GetFiles(MyPath);
			}
			catch(Exception  Err)
			{
				MessageBox.Show("读取文件信息发生错误!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);			
				return;
			}
			for (int j=0;j<MyFiles.Length;j  )
			{
				//添加新的子节点
				TreeNode MyNewNode= new TreeNode(MyFiles[j].ToString().Substring(MyFiles[j].ToString().LastIndexOf("\\") 1));
				//设置不选定状态下的图标
				MyNewNode.ImageIndex =3;
				//设置打开状态下的图标
				MyNewNode.SelectedImageIndex =3;
				node.Nodes.Add(MyNewNode);
			}
		
		}
		private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
		{//处理节点展开事件
			//获取发出信息的节点
			TreeNode CurrentNode=e.Node;
			//设置它为treeView的当前节点
			this.treeView1.SelectedNode=CurrentNode;
			string MyPath=CurrentNode.FullPath;			
			string[] MyDirs=null;
			try
			{
				//获得当前节点所有的子节点
				MyDirs=Directory.GetDirectories(MyPath);
			}
			catch(Exception  error)
			{
				MessageBox.Show("读取文件夹信息发生错误!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);			
				return;
			}            
			if (MyDirs!=null)
			{
				//为每一个当前节点的字节点添加子节点
				for (int j=0;j<MyDirs.Length;j  )
				{	
					//如果当前节点的子节点数为0,则为它添加子节点
					//否则说明已经添加过或则没有子文件夹
					if (CurrentNode.Nodes[j].Nodes.Count==0)
					{
						//调用函数为子节点添加子节点
						MyAddTreeNodeFunction(CurrentNode.Nodes[j],MyDirs[j]);
					}					
				}
			}			
		}
		private void button1_Click(object sender, System.EventArgs e)
		{//清空回收站里指定的文件或文件夹
			TreeNode CurrentNode=this.treeView1.SelectedNode;
            TreeNode MyDeletedNode=CurrentNode;
			if(CurrentNode==null)
			{
				MessageBox.Show("请首先选择要清空的文件夹或文件!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;			
			}			
			string MyPath=CurrentNode.Text;
			CurrentNode=CurrentNode.Parent;			   		
			while(CurrentNode!=null)
			{
			   string TempStr=CurrentNode.Text;		
			   TempStr ="\\" MyPath;
			   MyPath=TempStr;			   
			   CurrentNode=CurrentNode.Parent;			   
			}
			if(MyPath.Length<12)
			{
				MessageBox.Show("回收站文件夹不能被删除!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);			
				return;
			}		
			try
			{
				if(Directory.Exists(MyPath))
				{
					if(MessageBox.Show("是否清空文件夹:" MyPath,"信息提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
					{
						Directory.Delete(MyPath,true);
						MessageBox.Show("清空回收站里的文件夹:" MyPath " 操作成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
						this.treeView1.Nodes.Remove(MyDeletedNode);
						return;
					}
				}
				if(File.Exists(MyPath))
				{
					if(MessageBox.Show("是否清空文件:" MyPath,"信息提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
					{
						File.Delete(MyPath);
						MessageBox.Show("清空回收站里的文件:" MyPath " 操作成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
						this.treeView1.Nodes.Remove(MyDeletedNode);
						return;
					}
				}
			}
			catch(Exception Err)
			{
				MessageBox.Show("清空回收站操作发生错误:" Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);				
				return;
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{//清空回收站所有内容
			if(MessageBox.Show("是否清空系统回收站里面的所有内容?","信息提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.No)
			{
				return;
			}
			//获取计算机上逻辑驱动器的名称
			string[] drivers=Directory.GetLogicalDrives();
			int i=drivers.Length;        
			//对每个逻辑驱动器上的回收站都要清空
			foreach (string driver in drivers)
			{
				try
				{					
					string[] MyFiles=Directory.GetFiles(driver "\\Recycled");
					//首先清空回收站下单独的文件
					if (MyFiles.Length!=0)
					{
						foreach (string MyFile in MyFiles)
						{
							File.Delete(MyFile);
						} 
					}              
					string[] MyFolders=Directory.GetDirectories(driver "\\Recycled");
					//清空回收站下的文件夹
					if (MyFolders.Length!=0)
					{
						foreach (string MyFolder in MyFolders)
						{							
							Directory.Delete(MyFolder,true);
						}					
					}
				}
				catch(Exception Err)
				{
					//不加入任何的事件处理
				}
			}		
			if(this.treeView1.Nodes.Count>0)
				this.treeView1.Nodes.RemoveAt(0);			
			MessageBox.Show("清空回收站操作成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);				
	}
		private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{//选择不同逻辑驱动器中的回收站
			if(this.treeView1.Nodes.Count>0)
			  this.treeView1.Nodes.RemoveAt(0);
			string MyDriver=(string)this.comboBox1.SelectedItem;
			this.treeView1.Nodes.Add(new TreeNode(MyDriver "RECYCLED",1,1));
			string MyPath=MyDriver "RECYCLED";
			string[] MyDirs=null;
			string[] MyFiles=null;
			try
			{	//获得指定路径文件夹的名称
				MyDirs=Directory.GetDirectories(MyPath);
			}
			catch(Exception  Err)
			{
				MessageBox.Show("读取文件夹发生错误!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (MyDirs!=null)
			{
				//为每一个节点添加子节点
				for (int j=0;j<MyDirs.Length;j  )
				{
					//获得节点的去掉路径后的文件夹名
					TreeNode MyNewNode = new TreeNode(MyDirs[j].ToString().Substring(MyDirs[j].ToString().LastIndexOf("\\") 1));
					//设置不选定状态下的图标
					MyNewNode.ImageIndex =2;
					//设置打开状态下的图标
					MyNewNode.SelectedImageIndex =0;
					//添加节点
					this.treeView1.Nodes[0].Nodes.Add(MyNewNode);
				}
			}			
			try
			{	//获得指定路径文件夹的名称
				MyFiles=Directory.GetFiles(MyPath);
			}
			catch(Exception  Err)
			{
				MessageBox.Show("读取文件发生错误!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (MyFiles!=null)
			{
				//为每一个节点添加子节点
				for (int j=0;j<MyFiles.Length;j  )
				{
					//获得节点的去掉路径后的文件夹名
					TreeNode MyNewNode = new TreeNode(MyFiles[j].ToString().Substring(MyFiles[j].ToString().LastIndexOf("\\") 1));
					//设置不选定状态下的图标
					MyNewNode.ImageIndex =3;
					//设置打开状态下的图标
					MyNewNode.SelectedImageIndex =3;
					//添加节点
					this.treeView1.Nodes[0].Nodes.Add(MyNewNode);
				}
			}				
		}
	}
}