C#实现获取系统目录并以Tree树叉显示的方法
程序员文章站
2023-12-18 16:28:16
本文讲述c#获取windows系统目录,如何目录遍历以及将信息捆绑在treeview中显示出来的实现方法,具体实现代码如下:
using system;
usi...
本文讲述c#获取windows系统目录,如何目录遍历以及将信息捆绑在treeview中显示出来的实现方法,具体实现代码如下:
using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using system.io; namespace 获取系统目录 { public class form1 : system.windows.forms.form { private system.windows.forms.treeview treeview1; private system.componentmodel.icontainer components; private system.windows.forms.button button2; private system.windows.forms.label label2; private system.windows.forms.button button1; private system.windows.forms.textbox textbox1; private system.windows.forms.label label1; public form1() { initializecomponent(); filltree(treeview1, true); // 显示所有文件夹以及文件 } protected override void dispose( bool disposing ) { if( disposing ) { if (components != null) { components.dispose(); } } base.dispose( disposing ); } #region windows 窗体设计器生成的代码 private void initializecomponent() { this.textbox1 = new system.windows.forms.textbox(); this.label2 = new system.windows.forms.label(); this.label1 = new system.windows.forms.label(); this.treeview1 = new system.windows.forms.treeview(); this.button1 = new system.windows.forms.button(); this.button2 = new system.windows.forms.button(); this.suspendlayout(); this.textbox1.location = new system.drawing.point(280, 56); this.textbox1.multiline = true; this.textbox1.name = "textbox1"; this.textbox1.size = new system.drawing.size(208, 192); this.textbox1.tabindex = 3; this.textbox1.text = ""; this.label2.autosize = true; this.label2.location = new system.drawing.point(280, 24); this.label2.name = "label2"; this.label2.size = new system.drawing.size(116, 17); this.label2.tabindex = 4; this.label2.text = "文件夹或文件信息:"; this.label1.autosize = true; this.label1.location = new system.drawing.point(16, 24); this.label1.name = "label1"; this.label1.size = new system.drawing.size(153, 17); this.label1.tabindex = 5; this.label1.text = "请选择一个文件夹或文件:"; this.treeview1.imageindex = -1; this.treeview1.location = new system.drawing.point(16, 56); this.treeview1.name = "treeview1"; this.treeview1.selectedimageindex = -1; this.treeview1.size = new system.drawing.size(184, 192); this.treeview1.tabindex = 6; this.button1.location = new system.drawing.point(216, 104); this.button1.name = "button1"; this.button1.size = new system.drawing.size(48, 32); this.button1.tabindex = 7; this.button1.text = "显示"; this.button1.click += new system.eventhandler(this.button1_click); this.button2.location = new system.drawing.point(216, 168); this.button2.name = "button2"; this.button2.size = new system.drawing.size(48, 32); this.button2.tabindex = 8; this.button2.text = "清空"; this.button2.click += new system.eventhandler(this.button2_click); this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(504, 277); this.controls.add(this.button2); this.controls.add(this.button1); this.controls.add(this.treeview1); this.controls.add(this.label1); this.controls.add(this.label2); this.controls.add(this.textbox1); this.name = "form1"; this.text = "form1"; this.resumelayout(false); } #endregion [stathread] static void main() { application.run(new form1()); } // 填充目录和文件到 treeview 控件中 // issource 表示是否显示文件 private void filltree(treeview treeview,bool issource) { treeview.nodes.clear(); // 清空 // 获取系统上的所有逻辑驱动器 string[] strdrives = environment.getlogicaldrives(); foreach(string rootdirectoryname in strdrives) { try { // 获取驱动器*目录列表 directoryinfo dir = new directoryinfo(rootdirectoryname); // 如果获得的目录信息正确,则将它添加到 treeview 控件中 if (dir.exists == true) { treenode newnode = new treenode(rootdirectoryname); treeview.nodes.add(newnode); if (issource) { getsubdirectorynodes(newnode, newnode.text, true); } else { getsubdirectorynodes(newnode, newnode.text, false); } } } catch(exception e) { messagebox.show(e.message); } } } // 遍历子目录 private void getsubdirectorynodes(treenode parentnode, string fullname, bool getfilenames) { directoryinfo dir = new directoryinfo(fullname); directoryinfo[] subdirs = dir.getdirectories(); // 为每一个子目录添加一个子节点 foreach(directoryinfo subdir in subdirs) { // 不显示隐藏文件夹 if((subdir.attributes & fileattributes.hidden) != 0) { continue; } treenode subnode = new treenode(subdir.name); parentnode.nodes.add(subnode); // 递归调用getsubdirectorynodes getsubdirectorynodes(subnode, subdir.fullname, getfilenames); } // 获取目录中的文件 if(getfilenames) { fileinfo[] files = dir.getfiles(); foreach(fileinfo file in files) { treenode filenode = new treenode(file.name); parentnode.nodes.add(filenode); } } } private void button1_click(object sender, system.eventargs e) { try { treenode selectednode = treeview1.selectednode; directoryinfo info = new directoryinfo(selectednode.fullpath); string [] strarray = new string[4]; strarray[0] = "完整路径名 : "+ info.fullname; strarray[1] = "创建时间 : "+ info.creationtime.tostring(); strarray[2] = "上次访问时间 : "+ info.lastaccesstime.tostring(); strarray[3] = "上次写入时间 : "+ info.lastwritetime.tostring(); textbox1.lines = strarray; } catch(exception exc) { messagebox.show(exc.message); } } private void button2_click(object sender, system.eventargs e) { textbox1.text = ""; } } }