/// <summary> /// /// </summary> /// <param name="nod"></param> private void InitializeNode(FileTreeNode node) { DirectoryInfo directory = node.FileSystemInfo as DirectoryInfo; try { // Filter - Only Directories List <FileSystemInfo> files = FileMask.IncludeAttributes(directory.GetFileSystemInfos(), FileAttributes.Directory); BeginUpdate(); node.Nodes.Clear(); // create new nodes for each sub node int len = files.Count; for (int i = 0; i < len; ++i) { string name = files[i].Name; FileSystemInfo fi = (FileSystemInfo)files[i]; FileTreeNode child = new FileTreeNode(name, name, fi, 0, 1); node.Nodes.Add(child); } //Sort(); EndUpdate(); //node.Expand(); } catch (UnauthorizedAccessException) { //MessageBox.Show( "Can not access " + directory.FullName ); //node.ImageIndex = 8; //node.SelectedImageIndex = 8; //// goto parent node //SelectedNode = SelectedNode.Parent; return; } }
/// <summary> /// allow control to prepare to be updated /// </summary> /// <param name="drives"></param> /// <param name="files"></param> public void RefreshTree(FileTreeNode node) { DateTime dt = node.FileSystemInfo.LastWriteTime; node.FileSystemInfo.Refresh(); if (dt != node.FileSystemInfo.LastWriteTime) { InitializeNode(node); } }
//todo //protected override void OnBeforeExpand( TreeViewCancelEventArgs e ) //{ // base.OnBeforeExpand( e ); // change the focus // FileTreeNode node = (FileTreeNode)e.Node; // if(CanReadFile( node.FileSystemInfo )) // { // foreach(FileTreeNode n in node.Nodes) // { // Debug.WriteLine( n.Name ); // if(!n.Intialized) // InitializeNode( n ); // else // this.RefreshTree( n ); // } // } //} /// <summary> /// creates all sub nodes "only" before being selected /// </summary> /// <param name="e"></param> protected void Node_AfterSelect(object sender, TreeViewEventArgs e) { // change the focus FileTreeNode node = (FileTreeNode)e.Node; if (CanReadFile(node.FileSystemInfo)) { if (!node.Intialized) { InitializeNode(node); } else { this.RefreshTree(node); } } }
/// <summary> /// /// </summary> /// <param name="path"></param> public void Open(string path) { if (!string.IsNullOrEmpty(path)) { string[] dirs = path.Split(Path.DirectorySeparatorChar); TreeNodeCollection cur_nodes = this.Nodes; foreach (string dir in dirs) { TreeNode[] nodes = cur_nodes.Find(dir, false); if (nodes.Length == 1) { FileTreeNode node = (FileTreeNode)nodes[0]; if (!node.Intialized) { InitializeNode(node); } this.SelectedNode = node; this.SelectedNode.Expand(); cur_nodes = this.SelectedNode.Nodes; } } } }