Pack all directories on the system into the tree view

xiaoxiao2021-03-06  46

///

/// Fill directory files to Treeview /// 1. Get all logical drives on the system /// 2. Get the top-level directory list on each logical drive, and passed over the Licen Directory // / Note: This program refers to "C # program design" /// /// /// private void FillDirectoryTree (TreeView TVW, Bool Issource) {TVW.NODES.CLEAR ();

// Get all logical drives on the system String [] strDrives = Environment.getLogicalDrives ();

foreach (string rootDirectoryName in strDrives) {// if (! rootDirectoryName = @ "E: /") // continue; try {// get the drive top-level directory list DirectoryInfo dir = new DirectoryInfo (rootDirectoryName); dir.GetDirectories ();

TreeNode ndRoot = new TreeNode (rootDirectoryName); tvw.Nodes.Add (ndRoot); if (isSource) {GetSubDirectoryNodes (ndRoot, ndRoot.Text, true);} else {GetSubDirectoryNodes (ndRoot, ndRoot.Text, false);}} Catch (Exception E) {MessageBox.show (E.MESSAGE);}}}

// iterate subdirectory private void GetSubDirectoryNodes (TreeNode parentNode, string fullName, bool getFileNames) {DirectoryInfo dir = new DirectoryInfo (fullName); DirectoryInfo [] dirSubs = dir.GetDirectories ();

// add a subdirectory for each sub-point foreach (DirectoryInfo dirSub in dirSubs) {// do not show hidden folder if {continue ((dirSub.Attributes & FileAttributes.Hidden) = 0!);} TreeNode subNode = new TreeNode (dirSub .Name); ParentNode.Nodes.Add (Subnode);

// recursive call GetSubDirectoryNodes GetSubDirectoryNodes (subNode, dirSub.FullName, getFileNames);} // Get directory file if (getFileNames) {FileInfo [] files = dir.GetFiles (); foreach (FileInfo file in files) {TreeNode fileNode = New Treenode (file.name); ParentNode.Nodes.Add (filenode);}}}

转载请注明原文地址:https://www.9cbs.com/read-79903.html

New Post(0)