实例介绍
【实例简介】
【实例截图】
【实例截图】

【核心代码】
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); } } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论