Homemade A simple explorer----TreeView control
The first step, new Project, basic setup; (set as StartUp Project;view/toolbox/treeview)
Step two, start adding nodes
Add namespaces using System.IO;
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten usingSystem.IO; One A namespace_resoucemanager_ - { - Public Partial classForm1:form the { - PublicForm1 () - { - InitializeComponent (); + } - + Private voidForm1_Load (Objectsender, EventArgs e) A { at //here is the root path of the resource manager - stringStrroot =@"h:\ Self-made resource manager";//Path - createparent (strroot); - } - - Private voidCreateparent (stringstrroot) in { - //Create root node parent toTreeNode parent =NewTreeNode (); +DirectoryInfo di=NewDirectoryInfo (strroot); -Parent. text=di. Name; theParent. Tag =di. FullName; * $ //Add parent NodePanax Notoginseng TVRESOUCEMANAGER.NODES.ADD (parent); - the //Create child nodes + Createchild (strroot,parent); A //Expand All Nodes the parent. ExpandAll (); + - } $ $ Private voidCreatechild (stringpath,treenode Parent) - { -DirectoryInfo di =NewDirectoryInfo (path); the //all sub-folders -directoryinfo[] Dirs =di. GetDirectories ();Wuyi //Traversing subfolders the foreach(DirectoryInfo dirinchdirs) - { Wu //Create child nodes -TreeNode Child =NewTreeNode (); AboutChild. Text =dir. Name; $ //Child . Tag = dir. FullName; - - //Add child nodes - parent. Nodes.Add (child); A + //recursively implement multi-level folder traversal, create child nodes, add child nodes the Createchild (dir. Fullname,child); - $ //Add a file node the CreateFile (dir. Fullname,child); the } the } the - Private voidCreateFile (stringp, TreeNode child) in { theDirectoryInfo di =NewDirectoryInfo (p); the //all files under the path Aboutfileinfo[] Files =di. GetFiles (); the //Add all files under a path the foreach(FileInfo fileinchfiles) the { + //Create a node -TreeNode tn =NewTreeNode (); theTn. Text =file. Name;Bayi //TN. Tag = file. FullName; the the //Adding nodes - Child . Nodes.Add (TN); - } the } the the } the}
View Code
This basically completes the directory addition, can not add files, delete files, move files and other operations, but also need to continue efforts.
On the right is two text boxes, which can be used for text editing and so on (not full code).
First click on a node in the left TreeView to determine which node is clicked, if. doc or. txt can be edited (other file types such as PDF can write their own code OH).
First, the above code is about. Comment Cancellation for tag
1 Private voidTvresoucemanager_afterselect (Objectsender, TreeViewEventArgs e)2 {3 if(E.node.tag = =NULL)return;4 stringPath =e.node.tag.tostring ();5 if(Path. LastIndexOf (". doc") >0)6 {7 //if you clicked a. doc document, write the title on the text box8Txttitle.text =path.getfilenamewithoutextension (e.node.text);9 //The document content is written to the next text box and the read text operation is performed using the specified encoding rulesTen //Txtcontent.text = File.readalltext (path,encoding.getencoding ("Utf-8")); OneTxtcontent.text =file.readalltext (path, encoding.default); A } - - } the - Private voidBtnsave_click (Objectsender, EventArgs e) - { - if(Tvresoucemanager.selectednode = =NULL)return; + if(TvResouceManager.SelectedNode.Tag = =NULL)return; - + stringPath =tvResouceManager.SelectedNode.Tag.ToString (); A at if(Path. LastIndexOf (". doc") >0) - { - stringContent =Txtcontent.text; - file.writealltext (path, content, Encoding.default); - -MessageBox.Show ("Save successed"); in - } to}
View Code
Using Windows Form to make a simple explorer