private void treeViewFolders_BeforeExpand(object sender, TreeViewCancelEventArgs e) { FolderNode folderNode = e.Node as FolderNode; if (folderNode.ThisFolderInfo == null) { return; } folderNode.AddChildren(); }
/// <summary> /// Add the Folders to the parent /// Check for Files as well /// </summary> /// <param name="folders"></param> public void AddChildren() { if (ThisFolderInfo == null) { return; } System.IO.DirectoryInfo[] subDirs = null; // Now find all the subdirectories under this directory. try { subDirs = ThisFolderInfo.GetDirectories(); } catch (System.UnauthorizedAccessException e) { Debug.WriteLine(e.Message); } catch (System.IO.PathTooLongException e) { Debug.WriteLine(e.Message); } catch (IOException io) { Debug.WriteLine("IOException " + io.Message); } if (subDirs != null) { foreach (System.IO.DirectoryInfo f in subDirs) { FolderNode n = null; bool NewNode = false; if (_alreadyProcessedList.ContainsKey(f.FullName)) { n = _alreadyProcessedList[f.FullName]; } else { n = new FolderNode(f.Name, f); NewNode = true; } bool remove = false; n.AddSecondLevelChildren(out remove); if (!remove && NewNode) { this.Nodes.Add(n); } } } }
/// <summary> /// Run When the form is created /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { //rick 6-23-17 we are reworking this to look like //windows exploror _parent = FolderNode.InitRootTree(); treeViewFolders.Nodes.Add(_parent); //set the default search box captions textBoxSearchSS.Text = "Search Box"; textBoxSearchSS.ForeColor = SystemColors.InactiveCaption; textBoxSearchFiles.Text = "Search Box"; textBoxSearchFiles.ForeColor = SystemColors.InactiveCaption; // OLD NOT USING /* * //read the drives and set the current one to the last one used * // or the first one in the list that will * // most likely be C: * var driveInfo = DriveInfo.GetDrives(); * foreach(DriveInfo d in driveInfo) { * comboBoxDrives.Items.Add(d.Name); * } * * //check if the last saved is still in the list. * bool found = false; * foreach(string d in comboBoxDrives.Items) { * if(Properties.Settings.Default.LastDrive == d) { * found = true; * comboBoxDrives.Text = d; * break; * } * } * if(!found) { * comboBoxDrives.Text = comboBoxDrives.Items[0].ToString(); * } * * * //Populate the Folder Tree * InitFolderTree(comboBoxDrives.Text); * * * treeViewFolders.Nodes.Add(_parent); */ }
private void AddSecondLevelChildren(out bool remove) { System.IO.DirectoryInfo[] subDirs = null; // Now find all the subdirectories under this directory. remove = false; //change this when you can if (this.Text == "$Recycle.Bin") { remove = true; return; } try { subDirs = ThisFolderInfo.GetDirectories(); } catch (System.UnauthorizedAccessException e) { Debug.WriteLine(e.Message); remove = true; return; } catch (System.IO.PathTooLongException e) { Debug.WriteLine(e.Message); } #region Now Check For Files //now check for files System.IO.FileInfo[] files = null; // First, process all the files directly under this folder try { files = ThisFolderInfo.GetFiles("*.*"); } // This is thrown if even one of the files requires permissions greater // than the application provides. catch (UnauthorizedAccessException e) { // This code just writes out the message and continues to recurse. // You may decide to do something different here. For example, you // can try to elevate your privileges and access the file again. Debug.WriteLine(e.Message); remove = true; return; } catch (System.IO.DirectoryNotFoundException e) { Debug.WriteLine(e.Message); } catch (System.IO.PathTooLongException e) { Debug.WriteLine(e.Message); remove = true; return; } #endregion //Remove any files that are hidden List <FileInfo> tempList = new List <FileInfo>(); foreach (FileInfo fi in files) { if ((fi.Attributes & FileAttributes.Hidden) == 0) { tempList.Add(fi); } } //if the folder has NO sub-folders and no Files // remove it if ((files == null || files.Length == 0) && (subDirs == null || subDirs.Length == 0)) { remove = true; return; } if (subDirs != null) { foreach (System.IO.DirectoryInfo f in subDirs) { FolderNode n = null; if (_alreadyProcessedList.ContainsKey(f.FullName)) { n = _alreadyProcessedList[f.FullName]; } else { n = new FolderNode(f.Name, f); this.Nodes.Add(n); } } } }
/// <summary> /// This will create the parent tree structure /// Similar to what windows exploror looks like /// </summary> /// <returns></returns> public static FolderNode InitRootTree() { var root = new FolderNode("This PC"); var favorites = Environment.GetFolderPath(Environment.SpecialFolder.Favorites); if (Directory.Exists(favorites)) { var FavoritesInfo = new FolderNode(returnRootName(favorites), new DirectoryInfo(favorites)); FavoritesInfo.AddChildren(); root.Nodes.Add(FavoritesInfo); } var desk = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); if (Directory.Exists(desk)) { var deskInfo = new FolderNode(returnRootName(desk), new DirectoryInfo(desk)); deskInfo.AddChildren(); root.Nodes.Add(deskInfo); } var doc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (Directory.Exists(doc)) { var docInfo = new FolderNode(returnRootName(doc), new DirectoryInfo(doc)); docInfo.AddChildren(); root.Nodes.Add(docInfo); } var down = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); var down2 = Path.Combine(down, "Downloads"); if (Directory.Exists(down2)) { var downInfo = new FolderNode(returnRootName(down2), new DirectoryInfo(down2)); downInfo.AddChildren(); root.Nodes.Add(downInfo); } var vid = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos); if (Directory.Exists(vid)) { var vidInfo = new FolderNode(returnRootName(vid), new DirectoryInfo(vid)); vidInfo.AddChildren(); root.Nodes.Add(vidInfo); } var pic = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); if (Directory.Exists(pic)) { var picInfo = new FolderNode(returnRootName(pic), new DirectoryInfo(pic)); picInfo.AddChildren(); root.Nodes.Add(picInfo); } var music = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); if (Directory.Exists(music)) { var musicInfo = new FolderNode(returnRootName(music), new DirectoryInfo(music)); musicInfo.AddChildren(); root.Nodes.Add(musicInfo); } //var mycomp = Environment.GetFolderPath(Environment.SpecialFolder.); //var mycompInfo = new FolderNode(returnRootName(mycomp), new DirectoryInfo(mycomp)); //mycompInfo.AddChildren(); //root.Nodes.Add(mycomp); var home = Environment.GetEnvironmentVariable("USERPROFILE"); var dropBox = Path.Combine(home, "Dropbox"); if (Directory.Exists(dropBox)) { var dropboxInfo = new FolderNode(returnRootName("Dropbox"), new DirectoryInfo(dropBox)); dropboxInfo.AddChildren(); root.Nodes.Add(dropboxInfo); } var driveInfo = DriveInfo.GetDrives(); foreach (DriveInfo d in driveInfo) { var t = new FolderNode(d.RootDirectory.Name, new DirectoryInfo(d.RootDirectory.FullName)); t.AddChildren(); root.Nodes.Add(t); } return(root); }