public FileSystemBrowserForm(Machine machine) { InitializeComponent(); Machine = machine; SelectedPath = string.Empty; Description = "Select a file or folder..."; BrowserMode = Mode.File | Mode.Folder; Filter = DEFAULT_FILTER; _machineAvailable = ConnectionStore.ConnectionCreated(Machine); _entryTree = new Dictionary <IFileSystemEntry, IFileSystemEntry>(); _entryNodes = new Dictionary <IFileSystemEntry, FileSystemTreeNode>(); _entryFilter = new Dictionary <IFileSystemEntry, string>(); _entryQueue = new PrioritizedEntryQueue(); _pathExpansionQueue = null; _pathExpansionWaitCursor = null; _lastAutoExpandedNode = null; _machineNode = null; imageList.Images.Add(IMAGE_LIST_KEY_MACHINE, MakeIconImage(Resources.machine_16)); imageList.Images.Add(IMAGE_LIST_KEY_FOLDER, MakeIconImage(Resources.folder_16)); imageList.Images.Add(FileSystemDriveType.RemovableDisk.ToString(), MakeIconImage(Resources.drive_removable_disk_16)); imageList.Images.Add(FileSystemDriveType.LocalDisk.ToString(), MakeIconImage(Resources.drive_local_disk_16)); imageList.Images.Add(FileSystemDriveType.NetworkDrive.ToString(), MakeIconImage(Resources.drive_network_16)); imageList.Images.Add(FileSystemDriveType.CompactDisc.ToString(), MakeIconImage(Resources.drive_compact_disc_16)); }
private void RequestFiles(IFileSystemEntry entry) { if (!_machineAvailable) { return; } if (FolderMode) { return; } _entryFilter[entry] = GetSelectedFilter(); List <FileSystemEntry> childEntries = ConnectionStore.Connections[Machine].ServiceHandler.Service .GetFileSystemEntries(BuildPath(entry), _entryFilter[entry]).Select(x => x.FromDTO()) .Where(x => !x.IsFolder).OrderBy(x => x.Name).ToList(); _entryTree.Where(x => x.Value == entry && !x.Key.IsFolder).ToList().ForEach(x => _entryTree.Remove(x)); childEntries.ForEach(childEntry => _entryTree.Add(childEntry, entry)); FileSystemTreeNode treeNode = _entryNodes[entry]; treeNode.ChildEntries = treeNode.ChildEntries.Where(x => x.IsFolder).Concat(childEntries).ToList(); }
private void TreeView_AfterExpand(object sender, TreeViewEventArgs e) { FileSystemTreeNode node = (FileSystemTreeNode)e.Node; if (node.Entry != null) { Task.Run(() => TaskActionWrapper.Do(() => RequestFolders(node.ChildEntries))); } }
private void RequestFolders(IEnumerable <IFileSystemEntry> entries) { _entryQueue.Add(entries); AutoExpandPath(); IFileSystemEntry entry; while ((entry = _entryQueue.Take()) != null) { if (!_machineAvailable) { return; } FileSystemTreeNode treeNode = _entryNodes[entry]; if (NodeHasChildren(treeNode)) { continue; } _entryFilter[entry] = GetSelectedFilter(); List <FileSystemEntry> childEntries = ConnectionStore.Connections[Machine].ServiceHandler.Service .GetFileSystemEntries(BuildPath(entry), FolderMode ? null : _entryFilter[entry]).Select(x => x.FromDTO()) .OrderBy(x => x.Name).ToList(); childEntries.ForEach(childEntry => _entryTree.Add(childEntry, entry)); List <FileSystemTreeNode> childTreeNodes = childEntries .Where(childEntry => childEntry.IsFolder) .Select(childEntry => new FileSystemTreeNode(childEntry.Name) { ImageKey = IMAGE_LIST_KEY_FOLDER, SelectedImageKey = IMAGE_LIST_KEY_FOLDER, Entry = childEntry }) .ToList(); foreach (FileSystemTreeNode childTreeNode in childTreeNodes) { _entryNodes.Add(childTreeNode.Entry, childTreeNode); childTreeNode.Nodes.Add("dummy"); } treeNode.ChildEntries = childEntries.Cast <IFileSystemEntry>().ToList(); SetTreeViewNodes(treeNode, childTreeNodes); } }
public FileSystemTreeNode(string name, FileSystemTreeNode parent = null) : this() { int indexSeparator = name.IndexOf(PathCombine.Separator); if (indexSeparator >= 0) { AddNode(name.Substring(indexSeparator + 1)); _name = name.Substring(0, indexSeparator - 1); } else { _name = name; } _parent = parent; }
void CreateTree(FileSystemTreeNode rootNode) { IEnumerable<string> dirs = Directory.EnumerateDirectories(rootNode.Name); foreach (var dir in dirs) { var dirTempNode = new FileSystemTreeNode( dir, rootNode ); rootNode.AddNode(dirTempNode); CreateTree(dirTempNode); } IEnumerable<string> files = Directory.EnumerateFiles(rootNode.Name); foreach (var file in files) { rootNode.AddNode(new FileSystemTreeNode(file, rootNode)); } }
/// <summary> /// Loads the grand children of the given node. /// </summary> /// <param name="p_tndFolder">The node whose grand children are to be loaded.</param> protected void LoadGrandChildren(FileSystemTreeNode p_tndFolder) { Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; try { foreach (FileSystemTreeNode tndFolder in p_tndFolder.Nodes) { LoadChildren(tndFolder); } } finally { Cursor = crsOldCursor; } }
/// <summary> /// Adds a virtual file system item to the tree view. /// </summary> /// <param name="p_tndRoot">The node to which to add the new item.</param> /// <param name="p_strName">The name of the new virtual item to add.</param> /// <returns>The new tree node representing the new virtual file system item.</returns> public FileSystemTreeNode AddVirtualNode(FileSystemTreeNode p_tndRoot, string p_strName) { if ((p_tndRoot != null) && !p_tndRoot.IsDirectory) return AddVirtualNode(p_tndRoot.Parent, p_strName); FileSystemTreeNode tndFile = null; System.Windows.Forms.TreeNodeCollection tncSiblings = (p_tndRoot == null) ? this.Nodes : p_tndRoot.Nodes; if (!tncSiblings.ContainsKey(p_strName)) tndFile = (FileSystemTreeNode)tncSiblings[p_strName]; else { string strName = Path.GetFileName(p_strName); if (String.IsNullOrEmpty(strName)) strName = p_strName; tndFile = new FileSystemTreeNode(strName, null); tndFile.Name = p_strName; tncSiblings.Add(tndFile); OnNodeAdded(new NodesChangedEventArgs(tndFile)); } return tndFile; }
private void DisplayFiles(FileSystemTreeNode node) { using (new WaitCursor(this, SetCursor)) { if (node == _machineNode) { SetListViewItems(node.ChildEntries .Where(x => x is FileSystemDrive) .Cast <FileSystemDrive>() .OrderBy(x => x.Name) .Select(x => new FileSystemListViewItem(new[] { MakeDriveLabel(x) }) { ImageKey = x.Type.ToString(), Entry = x })); } else { if (_entryFilter[node.Entry] != GetSelectedFilter()) { RequestFiles(node.Entry); } LoadFileIcons(node.ChildEntries); SetListViewItems(node.ChildEntries .Where(x => x is FileSystemEntry) .Cast <FileSystemEntry>() .OrderByDescending(x => x.IsFolder) .ThenBy(x => x.Name) .Select(x => new FileSystemListViewItem(new[] { x.Name, MakeFileSize(x), MakeFileDate(x) }) { ImageKey = x.IsFolder ? IMAGE_LIST_KEY_FOLDER : Path.GetExtension(x.Name), Entry = x })); } } AutoExpandPath(); }
private void ListView_DoubleClick(object sender, EventArgs e) { if (listView.SelectedItems.Count != 1) { return; } IFileSystemEntry entry = ((FileSystemListViewItem)listView.SelectedItems[0]).Entry; if (entry.IsFolder) { FileSystemTreeNode treeNode = _entryNodes[entry]; Task.Run(() => TaskActionWrapper.Do(() => { ExpandTreeNode(treeNode.Parent); SelectTreeNode(treeNode); })); } else { buttonOK.PerformClick(); } }
private void FileSystemBrowserForm_Load(object sender, EventArgs e) { ProcessManagerServiceConnectionHandler.Instance.ServiceHandlerConnectionChanged += ServiceConnectionHandler_ServiceHandlerConnectionChanged; labelDescription.Text = Description; _machineNode = new FileSystemTreeNode(Machine.ToString()) { ImageKey = IMAGE_LIST_KEY_MACHINE, SelectedImageKey = IMAGE_LIST_KEY_MACHINE }; treeView.Nodes.Add(_machineNode); PopulateFilter(); if (FolderMode) { splitContainer.Panel2Collapsed = true; Size = new Size(400, 420); } else { Size = new Size(700, 420); } EnableControls(); if (!string.IsNullOrEmpty(SelectedPath)) { PreparePathExpansion(SelectedPath); } Task.Run(() => TaskActionWrapper.Do(() => { DisplayDrives(); ExpandTreeNode(_machineNode); })); }
/// <summary> /// Populates the given node with its children. /// </summary> /// <param name="p_tndNode">The node to populate with children.</param> protected void PopulateNodeWithChildren(FileSystemTreeNode p_tndNode) { string strSource = null; if (!p_tndNode.IsDirectory) return; foreach (FileSystemTreeNode.Source srcSource in p_tndNode.Sources) { if (srcSource.IsLoaded) continue; strSource = srcSource.Path; srcSource.IsLoaded = true; if (strSource.StartsWith(Archive.ARCHIVE_PREFIX)) { KeyValuePair<string, string> kvpPath = Archive.ParseArchivePath(strSource); Archive arcArchive = new Archive(kvpPath.Key); string[] strFolders = arcArchive.GetDirectories(kvpPath.Value); for (Int32 i = 0; i < strFolders.Length; i++) addFomodFile(p_tndNode, Archive.GenerateArchivePath(kvpPath.Key, strFolders[i])); string[] strFiles = arcArchive.GetFiles(kvpPath.Value); for (Int32 i = 0; i < strFiles.Length; i++) addFomodFile(p_tndNode, Archive.GenerateArchivePath(kvpPath.Key, strFiles[i])); } else if (!strSource.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { string[] strFolders = Directory.GetDirectories(strSource); for (Int32 i = 0; i < strFolders.Length; i++) addFomodFile(p_tndNode, strFolders[i]); string[] strFiles = Directory.GetFiles(strSource); for (Int32 i = 0; i < strFiles.Length; i++) addFomodFile(p_tndNode, strFiles[i]); } } }
/// <summary> /// Copies the tree rooted at the given node. /// </summary> /// <param name="p_tndSource">The root of the tree to copy.</param> /// <returns>The root of the copied tree.</returns> private FileSystemTreeNode CopyTree(FileSystemTreeNode p_tndSource) { var tndDest = new FileSystemTreeNode(p_tndSource); CopyTree(p_tndSource, tndDest); return tndDest; }
/// <summary> /// Copies the tree rooted at the given source node to the tree rooted /// at the given destination node. /// </summary> /// <param name="p_tndSource">The root of the tree to copy.</param> /// <param name="p_tndDest">The root of the tree to which to copy.</param> private void CopyTree(FileSystemTreeNode p_tndSource, FileSystemTreeNode p_tndDest) { foreach (FileSystemTreeNode tndSourceNode in p_tndSource.Nodes) { var tndCopy = new FileSystemTreeNode(tndSourceNode); p_tndDest.Nodes.Add(tndCopy); CopyTree(tndSourceNode, tndCopy); } }
/// <summary> /// Determines if the children of the given node should be loaded. /// </summary> /// <param name="p_fsnItem">The node for which it is to be determined if the children should be loaded.</param> /// <returns><c>true</c> if teh children should be loaded; /// <c>false</c> otherwise.</returns> protected override bool ShouldLoadChildren(FileSystemTreeNode p_fsnItem) { return (p_fsnItem.Parent == null) || (p_fsnItem.Parent.IsExpanded); }
/// <summary> /// The recursive method that searches the fomod file structure for files in the specified directory /// matching the given pattern. /// </summary> /// <param name="p_tndRoot">The node from which to being searching.</param> /// <param name="p_queDirectories">The path to the directory in which to search.</param> /// <param name="p_rgxFileNamePattern">The pattern of the files to find.</param> /// <returns> /// Returns pairs of values representing the found files. The key of the pair is the fomod file path, /// and the value is the source path for the file. /// </returns> private List<KeyValuePair<string, string>> FindFomodFiles(FileSystemTreeNode p_tndRoot, Queue<string> p_queDirectories, Regex p_rgxFileNamePattern) { var lstMatches = new List<KeyValuePair<string, string>>(); if (p_tndRoot.IsDirectory && ((p_queDirectories.Count > 0) && p_tndRoot.Name.Equals(p_queDirectories.Peek()))) { p_queDirectories.Dequeue(); PopulateNodeWithChildren(p_tndRoot); var intOriginalDepth = p_queDirectories.Count; foreach (FileSystemTreeNode tndNode in p_tndRoot.Nodes) { lstMatches.AddRange(FindFomodFiles(tndNode, p_queDirectories, p_rgxFileNamePattern)); if (intOriginalDepth != p_queDirectories.Count) { break; } } } else if ((p_queDirectories.Count == 0) && p_rgxFileNamePattern.IsMatch(p_tndRoot.Name)) { lstMatches.Add(new KeyValuePair<string, string>(p_tndRoot.FullPath, p_tndRoot.LastSource)); } return lstMatches; }
public FileSystemTreeNode(FileSystemTreeNode node) { _name = node.Name; _children = new Dictionary<string, FileSystemTreeNode>(node.Children); _parent = new FileSystemTreeNode(node.Parent); }
/// <summary> /// A simple constructor that initializes the object with the given values. /// </summary> /// <param name="p_tndNode">The node that has changed.</param> public NodesChangedEventArgs(FileSystemTreeNode p_tndNode) { ChangedNode = p_tndNode; }
/// <summary> /// Determines if the children of the given node should be loaded. /// </summary> /// <param name="p_fsnItem">The node for which it is to be determined if the children should be loaded.</param> /// <returns><c>true</c> if teh children should be loaded; /// <c>false</c> otherwise.</returns> protected virtual bool ShouldLoadChildren(FileSystemTreeNode p_fsnItem) { return true; }
/// <summary> /// Addes the specified files to the source tree. /// </summary> /// <param name="p_tndRoot">The node to which to add the file/folder.</param> /// <param name="p_strFileNames">The paths to add to the source tree.</param> public void AddPaths(FileSystemTreeNode p_tndRoot, string[] p_strFileNames) { Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; foreach (string strFile in p_strFileNames) AddPath(p_tndRoot, strFile); Cursor = crsOldCursor; }
FileSystemTreeNode GetDiferent(FileSystemTreeNode thisTreeNode, FileSystemTreeNode otherTreeNode, Diferent flags) { FileSystemTreeNode returnNode = null; bool isDid = false; if (otherTreeNode == null) { if (flags.HasFlag(Diferent.Deleted)) { return new FileSystemTreeNode(thisTreeNode.Name.FullName, null); isDid = true; } else { throw new Exception("otherTreeNode == null"); } } if (thisTreeNode == null) { if (flags.HasFlag(Diferent.LastCreated)) { return returnNode = new FileSystemTreeNode(otherTreeNode.Name.FullName, null); isDid = true; } else throw new Exception("ThisTreeNode == null"); } returnNode = new FileSystemTreeNode(thisTreeNode.Name.FullName, null); foreach (var child in thisTreeNode.Children) { FileSystemTreeNode tempNode = GetDiferent(child.Value, otherTreeNode.Children[child.Key], flags); if (tempNode != null) returnNode.AddNode(tempNode); } if (returnNode.Children.Count == 0) returnNode = null; else { isDid = true; } if (flags.HasFlag(Diferent.Deleted) && !isDid) if (!File.Exists(otherTreeNode.Name.FullName) && (!Directory.Exists(otherTreeNode.Name.FullName))) { returnNode = new FileSystemTreeNode(otherTreeNode.Name.FullName, null); isDid = true; } if (flags.HasFlag(Diferent.LastModified) && !isDid) { if (thisTreeNode.Name.LastWriteTime > otherTreeNode.Name.LastWriteTime) { returnNode = new FileSystemTreeNode(thisTreeNode.Name.FullName, null); isDid = true; } else if (thisTreeNode.Name.LastWriteTime < otherTreeNode.Name.LastWriteTime) { returnNode = new FileSystemTreeNode(otherTreeNode.Name.FullName, null); isDid = true; } } if (flags.HasFlag(Diferent.LastCreated) && !isDid) { if (thisTreeNode.Name.CreationTime > otherTreeNode.Name.CreationTime) { returnNode = new FileSystemTreeNode(thisTreeNode.Name.FullName, null); isDid = true; } else if (thisTreeNode.Name.CreationTime < otherTreeNode.Name.CreationTime) { returnNode = new FileSystemTreeNode(otherTreeNode.Name.FullName, null); isDid = true; } } return returnNode; }
/// <summary> /// This adds a file/folder to the fomod file structure. /// </summary> /// <param name="p_tndRoot">The node to which to add the file/folder.</param> /// <param name="p_strFile">The path to add to the fomod file structure.</param> /// <returns> /// The node that was added for the specified file/folder. <lang langref="null" /> /// is returned if the given path is invalid. /// </returns> private FileSystemTreeNode addFomodFile(TreeNode p_tndRoot, string p_strFile) { if (!p_strFile.StartsWith(Archive.ARCHIVE_PREFIX) && !p_strFile.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { FileSystemInfo fsiInfo; if (Directory.Exists(p_strFile)) { fsiInfo = new DirectoryInfo(p_strFile); } else if (File.Exists(p_strFile)) { fsiInfo = new FileInfo(p_strFile); } else { return null; } if ((fsiInfo.Attributes & FileAttributes.System) > 0) { return null; } } var strFileName = Path.GetFileName(p_strFile); FileSystemTreeNode tndFile; var tncSiblings = (p_tndRoot == null) ? tvwFomod.Nodes : p_tndRoot.Nodes; if (tncSiblings.ContainsKey(strFileName.ToLowerInvariant())) { tndFile = (FileSystemTreeNode) tncSiblings[strFileName.ToLowerInvariant()]; tndFile.AddSource(p_strFile, false); } else { tndFile = new FileSystemTreeNode(strFileName, p_strFile); tndFile.ContextMenuStrip = cmsFomodNode; tndFile.Name = strFileName.ToLowerInvariant(); tncSiblings.Add(tndFile); } if (tndFile.IsDirectory) { tndFile.ImageKey = "folder"; tndFile.SelectedImageKey = "folder"; if ((p_tndRoot == null) || (p_tndRoot.IsExpanded)) { PopulateNodeWithChildren(tndFile); } } else { tndFile.Sources[p_strFile].IsLoaded = true; var strExtension = Path.GetExtension(p_strFile).ToLowerInvariant(); if (!imlIcons.Images.ContainsKey(strExtension)) { var strIconPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) + strExtension; File.CreateText(strIconPath).Close(); imlIcons.Images.Add(strExtension, Icon.ExtractAssociatedIcon(strIconPath)); File.Delete(strIconPath); } tndFile.ImageKey = strExtension; tndFile.SelectedImageKey = strExtension; } return tndFile; }
public FileSystemTree(string root) { _root = new FileSystemTreeNode(root, null); }
/// <summary> /// Finds the specified node, or the specified node's nearest ancestor if the specified /// node does not exist. /// </summary> /// <param name="p_tndRoot">The node under which to search for the specified node.</param> /// <param name="p_strPath">The path to the node to be found.</param> /// <param name="p_queMissingPath">An out parameter that returns the path parts that were not found.</param> /// <returns>The specified node, it if exists. If it does not exist, then the specified node's /// neasrest ancestor. If no ancestor exists, then <c>null</c> is returned.</returns> protected FileSystemTreeNode FindNearestAncestor(FileSystemTreeNode p_tndRoot, string p_strPath, out Queue<string> p_queMissingPath) { string[] strPaths = p_strPath.Split(new char[] { Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); if (strPaths[0].EndsWith(Path.VolumeSeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase)) strPaths[0] = strPaths[0] + Path.DirectorySeparatorChar; Queue<string> quePath = new Queue<string>(strPaths); FileSystemTreeNode tndSelectedNode = null; System.Windows.Forms.TreeNodeCollection tncCurrentLevel = (p_tndRoot == null) ? Nodes : p_tndRoot.Nodes; string strCurrentPath = null; while (quePath.Count > 0) { strCurrentPath = quePath.Peek(); foreach (FileSystemTreeNode tndCurrent in tncCurrentLevel) { if (tndCurrent.Text.Equals(strCurrentPath, StringComparison.OrdinalIgnoreCase)) { tndSelectedNode = tndCurrent; break; } } if ((tndSelectedNode == null) || (tncCurrentLevel == tndSelectedNode.Nodes)) break; tncCurrentLevel = tndSelectedNode.Nodes; quePath.Dequeue(); } p_queMissingPath = quePath; return tndSelectedNode; }
public void AddNode(FileSystemTreeNode newNode) { newNode._parent = this; _children.Add(newNode.Name,newNode); }
/// <summary> /// Processes the tree rooted at the given node to romve any superfluous nodes and sources. /// </summary> /// <remarks> /// This method cleans up the given tree so that the most efficient set of mappings /// needed to create the fomod file structure can be generated. /// </remarks> /// <param name="p_tndNode">The node at which the fomod file structure tree is rooted.</param> private void ProcessTree(FileSystemTreeNode p_tndNode) { if (p_tndNode.Nodes.Count == 0) { for (var j = p_tndNode.Sources.Count - 1; j >= 0; j--) { if (p_tndNode.Sources[j].Path.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { p_tndNode.Sources.RemoveAt(j); } } return; } foreach (FileSystemTreeNode tndNode in p_tndNode.Nodes) { ProcessTree(tndNode); } var lstSubPaths = new List<string>(); for (var j = p_tndNode.Sources.Count - 1; j >= 0; j--) { var srcSource = p_tndNode.Sources[j]; lstSubPaths.Clear(); if (srcSource.Path.StartsWith(Archive.ARCHIVE_PREFIX)) { var kvpPath = Archive.ParseArchivePath(srcSource.Path); var arcArchive = new Archive(kvpPath.Key); foreach (var strPath in arcArchive.GetDirectories(kvpPath.Value)) { lstSubPaths.Add(Archive.GenerateArchivePath(kvpPath.Key, strPath)); } foreach (var strPath in arcArchive.GetFiles(kvpPath.Value)) { lstSubPaths.Add(Archive.GenerateArchivePath(kvpPath.Key, strPath)); } } else if (srcSource.Path.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { p_tndNode.Sources.RemoveAt(j); continue; } else { lstSubPaths.AddRange(Directory.GetDirectories(srcSource.Path)); foreach (var strPath in Directory.GetFiles(srcSource.Path)) { if ((new FileInfo(strPath).Attributes & FileAttributes.System) > 0) { continue; } lstSubPaths.AddRange(Directory.GetFiles(srcSource.Path)); } } //if the source hasn't been loaded, then we treat it as if all // subpaths are present and have already been removed from // the children nodes, so we don't have to do anything if (srcSource.IsLoaded) { //if we find all the current folder's subpaths, and each subpath // has no children in the same source tree, then we can just copy // the current folder instead of copying each child individually var intFoundCount = 0; //so, for each subpath of the current folder... foreach (var strSubPath in lstSubPaths) { //...look through all the children nodes for the subpath... foreach (FileSystemTreeNode tndChild in p_tndNode.Nodes) { //...if we find the subpath... if (tndChild.Sources.Contains(strSubPath)) { //...and the node containing the subpath has no children // containing anything in the same source tree... var booFound = false; foreach (FileSystemTreeNode tndSubNode in tndChild.Nodes) { foreach (string strSubSource in tndSubNode.Sources) { if (strSubSource.StartsWith(strSubPath)) { booFound = true; break; } } if (booFound) { break; } } //...then we found the subpath. // if the node containing the subpath had had children containing // something in the same source tree, that would imply we aren't // copying all the current folder's descendants, so we would have to // copy each descendent individually, instead of just copying this folder if (!booFound) { intFoundCount++; } break; } } } //if we found all the subpaths... if (intFoundCount == lstSubPaths.Count) { //...then remove the subpaths, so we just copy the // current folder instead of copying each child individually foreach (var strSubPath in lstSubPaths) { for (var i = p_tndNode.Nodes.Count - 1; i >= 0; i--) { var tndNode = (FileSystemTreeNode) p_tndNode.Nodes[i]; if (tndNode.Sources.Contains(strSubPath)) { //if we are removing the last source, and there are no // children nodes (implying this node isn't needed in // another source tree), then prune this node away... if ((tndNode.Nodes.Count == 0) && (tndNode.Sources.Count <= 1)) { p_tndNode.Nodes.RemoveAt(i); } else //...otherwise just remove the source { tndNode.Sources.Remove(strSubPath); } break; } } } } else { //...else if we only found some of the subpaths // then remove the current folder from the sources so // it doesn't get copied: the current folder will be // created when the subpaths are copied... //...else if we found no subpaths then we remove the current folder // to prune empty folders p_tndNode.Sources.RemoveAt(j); } } } }
/// <summary> /// This adds a file/folder to the source file structure. /// </summary> /// <param name="p_tndRoot">The node to which to add the file/folder.</param> /// <param name="p_strFile">The path to add to the source file structure.</param> private void addSourceFile(FileSystemTreeNode p_tndRoot, string p_strFile) { if (!p_strFile.StartsWith(Archive.ARCHIVE_PREFIX) && !p_strFile.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { FileSystemInfo fsiInfo = null; if (Directory.Exists(p_strFile)) fsiInfo = new DirectoryInfo(p_strFile); else if (File.Exists(p_strFile)) fsiInfo = new FileInfo(p_strFile); else return; if ((fsiInfo.Attributes & FileAttributes.System) > 0) return; } FileSystemTreeNode tndFile = null; TreeNodeCollection tncSiblings = (p_tndRoot == null) ? tvwSource.Nodes : p_tndRoot.Nodes; if (tncSiblings.ContainsKey(p_strFile)) tndFile = (FileSystemTreeNode)tncSiblings[p_strFile]; else { tndFile = new FileSystemTreeNode(Path.GetFileName(p_strFile), p_strFile); tndFile.Name = p_strFile; tncSiblings.Add(tndFile); } if (tndFile.IsDirectory) { tndFile.ImageKey = "folder"; tndFile.SelectedImageKey = "folder"; if ((p_tndRoot == null) || (p_tndRoot.IsExpanded)) { tndFile.Sources[p_strFile].IsLoaded = true; if (p_strFile.StartsWith(Archive.ARCHIVE_PREFIX)) { KeyValuePair<string, string> kvpPath = Archive.ParseArchivePath(p_strFile); Archive arcArchive = new Archive(kvpPath.Key); string[] strFolders = arcArchive.GetDirectories(kvpPath.Value); for (Int32 i = 0; i < strFolders.Length; i++) addSourceFile(tndFile, Archive.GenerateArchivePath(kvpPath.Key, strFolders[i])); string[] strFiles = arcArchive.GetFiles(kvpPath.Value); for (Int32 i = 0; i < strFiles.Length; i++) addSourceFile(tndFile, Archive.GenerateArchivePath(kvpPath.Key, strFiles[i])); } else if (!p_strFile.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { string[] strFolders = Directory.GetDirectories(p_strFile); for (Int32 i = 0; i < strFolders.Length; i++) addSourceFile(tndFile, strFolders[i]); string[] strFiles = Directory.GetFiles(p_strFile); for (Int32 i = 0; i < strFiles.Length; i++) addSourceFile(tndFile, strFiles[i]); } } } else { tndFile.Sources[p_strFile].IsLoaded = true; string strExtension = Path.GetExtension(p_strFile).ToLowerInvariant(); if (!imlIcons.Images.ContainsKey(strExtension)) { string strIconPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) + strExtension; File.CreateText(strIconPath).Close(); imlIcons.Images.Add(strExtension, System.Drawing.Icon.ExtractAssociatedIcon(strIconPath)); File.Delete(strIconPath); } tndFile.ImageKey = strExtension; tndFile.SelectedImageKey = strExtension; if (tndFile.IsArchive) { Archive arcArchive = new Archive(p_strFile); string[] strFolders = arcArchive.GetDirectories("/"); for (Int32 i = 0; i < strFolders.Length; i++) addSourceFile(tndFile, Archive.GenerateArchivePath(p_strFile, strFolders[i])); string[] strFiles = arcArchive.GetFiles("/"); for (Int32 i = 0; i < strFiles.Length; i++) addSourceFile(tndFile, Archive.GenerateArchivePath(p_strFile, strFiles[i])); } } }
/// <summary> /// Populates the given node with its children. /// </summary> /// <param name="p_tndNode">The node to populate with children.</param> protected void PopulateNodeWithChildren(FileSystemTreeNode p_tndNode) { if (!p_tndNode.IsDirectory) { return; } foreach (var srcSource in p_tndNode.Sources) { if (srcSource.IsLoaded) { continue; } var strSource = srcSource.Path; srcSource.IsLoaded = true; if (strSource.StartsWith(Archive.ARCHIVE_PREFIX)) { var kvpPath = Archive.ParseArchivePath(strSource); var arcArchive = new Archive(kvpPath.Key); var strFolders = arcArchive.GetDirectories(kvpPath.Value); foreach (var folder in strFolders) { addFomodFile(p_tndNode, Archive.GenerateArchivePath(kvpPath.Key, folder)); } var strFiles = arcArchive.GetFiles(kvpPath.Value); foreach (var file in strFiles) { addFomodFile(p_tndNode, Archive.GenerateArchivePath(kvpPath.Key, file)); } } else if (!strSource.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { var strFolders = Directory.GetDirectories(strSource); foreach (var folder in strFolders) { addFomodFile(p_tndNode, folder); } var strFiles = Directory.GetFiles(strSource); foreach (var file in strFiles) { addFomodFile(p_tndNode, file); } } } }
/// <summary> /// This adds a file/folder to the source file structure. /// </summary> /// <param name="p_tndRoot">The node to which to add the file/folder.</param> /// <param name="p_strFile">The path to add to the source file structure.</param> public FileSystemTreeNode AddPath(FileSystemTreeNode p_tndRoot, string p_strFile) { if ((p_tndRoot != null) && !p_tndRoot.IsDirectory && (!BrowseIntoArchives || !p_tndRoot.IsArchive)) return AddPath(p_tndRoot.Parent, p_strFile); Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; try { if (!Archive.IsArchivePath(p_strFile)) { FileSystemInfo fsiInfo = null; if (Directory.Exists(p_strFile)) fsiInfo = new DirectoryInfo(p_strFile); else if (ShowFiles && File.Exists(p_strFile) && !".lnk".Equals(Path.GetExtension(p_strFile), StringComparison.OrdinalIgnoreCase)) { fsiInfo = new FileInfo(p_strFile); } else return null; if (!FileUtil.IsDrivePath(p_strFile) && ((fsiInfo.Attributes & FileAttributes.System) > 0)) return null; } FileSystemTreeNode tndFile = null; System.Windows.Forms.TreeNodeCollection tncSiblings = (p_tndRoot == null) ? this.Nodes : p_tndRoot.Nodes; string strName = Path.GetFileName(p_strFile); if (String.IsNullOrEmpty(strName)) strName = p_strFile; if (tncSiblings.ContainsKey(strName)) { tndFile = (FileSystemTreeNode)tncSiblings[strName]; tndFile.AddSource(p_strFile, false); } else { tndFile = new FileSystemTreeNode(strName, p_strFile); tndFile.Name = strName; tncSiblings.Add(tndFile); OnNodeAdded(new NodesChangedEventArgs(tndFile)); } SetNodeImage(tndFile); if (tndFile.IsDirectory) { if (ShouldLoadChildren(tndFile)) LoadChildren(tndFile); } else { if (BrowseIntoArchives && tndFile.IsArchive) { if (ShouldLoadChildren(tndFile)) LoadChildren(tndFile); } else tndFile.Sources[p_strFile].IsLoaded = true; } return tndFile; } finally { Cursor = crsOldCursor; } }
/// <summary> /// Addes the specified virtual paths to the source tree. /// </summary> /// <param name="p_tndRoot">The node to which to add the paths.</param> /// <param name="p_vfiPaths">The paths to add to the source tree.</param> public void AddVirtualPaths(FileSystemTreeNode p_tndRoot, VirtualFileSystemItem[] p_vfiPaths) { Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; foreach (VirtualFileSystemItem vfiPath in p_vfiPaths) AddVirtualPath(p_tndRoot, vfiPath); Cursor = crsOldCursor; }
/// <summary> /// Loads the children of the given node. /// </summary> /// <param name="p_tndFolder">The node whose children are to be loaded.</param> protected void LoadChildren(FileSystemTreeNode p_tndFolder) { if (p_tndFolder.LastSource.IsLoaded || !p_tndFolder.IsDirectory && (!BrowseIntoArchives || !p_tndFolder.IsArchive)) return; Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; try { p_tndFolder.LastSource.IsLoaded = true; string strPath = p_tndFolder.LastSource; if (BrowseIntoArchives && (Archive.IsArchivePath(strPath) || p_tndFolder.IsArchive)) { KeyValuePair<string, string> kvpPath = new KeyValuePair<string, string>(p_tndFolder.LastSource, Path.DirectorySeparatorChar.ToString()); if (Archive.IsArchivePath(strPath)) kvpPath = Archive.ParseArchivePath(strPath); Archive arcArchive = new Archive(kvpPath.Key); string[] strFolders = arcArchive.GetDirectories(kvpPath.Value); for (Int32 i = 0; i < strFolders.Length; i++) AddPath(p_tndFolder, Archive.GenerateArchivePath(kvpPath.Key, strFolders[i])); if (ShowFiles) { string[] strFiles = arcArchive.GetFiles(kvpPath.Value, false); for (Int32 i = 0; i < strFiles.Length; i++) AddPath(p_tndFolder, Archive.GenerateArchivePath(kvpPath.Key, strFiles[i])); } } else { try { string[] strFolders = Directory.GetDirectories(strPath); for (Int32 i = 0; i < strFolders.Length; i++) AddPath(p_tndFolder, strFolders[i]); if (ShowFiles) { string[] strFiles = Directory.GetFiles(strPath); for (Int32 i = 0; i < strFiles.Length; i++) AddPath(p_tndFolder, strFiles[i]); } } catch (UnauthorizedAccessException) { } } } finally { Cursor = crsOldCursor; } }
/// <summary> /// Addes the specified virtual path to the source tree. /// </summary> /// <param name="p_tndRoot">The node to which to add the path.</param> /// <param name="p_vfiPath">The path to add to the source tree.</param> public void AddVirtualPath(FileSystemTreeNode p_tndRoot, VirtualFileSystemItem p_vfiPath) { Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; Queue<string> queRemainingPath = null; FileSystemTreeNode tndNode = FindNearestAncestor(p_tndRoot, p_vfiPath.Path, out queRemainingPath); string strCurrentPath = null; while (queRemainingPath.Count > 0) { strCurrentPath = queRemainingPath.Dequeue(); if ((queRemainingPath.Count > 0) || p_vfiPath.IsDirectory) { FileSystemTreeNode tndChild = new FileSystemTreeNode(strCurrentPath, null); ((tndNode == null) ? Nodes : tndNode.Nodes).Add(tndChild); OnNodeAdded(new NodesChangedEventArgs(tndChild)); tndNode = tndChild; } else { tndNode = AddPath(tndNode, p_vfiPath.Source); } } tndNode.AddSource(p_vfiPath.Source, true); Cursor = crsOldCursor; }
/// <summary> /// Sets the image of the given node. /// </summary> /// <param name="p_fsnItem">The node whose image shuld be set.</param> protected virtual void SetNodeImage(FileSystemTreeNode p_fsnItem) { if (p_fsnItem.IsDirectory) { p_fsnItem.ImageKey = "folder"; p_fsnItem.SelectedImageKey = "folder"; } else { string strExtension = Path.GetExtension(p_fsnItem.LastSource).ToLowerInvariant(); if (!ImageList.Images.ContainsKey(strExtension)) { //this method should work, and it should be faster; however, it seems to // always return the generic file icon - no idea why /*if (!Archive.IsArchivePath(p_fsnItem.LastSource)) ImageList.Images.Add(strExtension, System.Drawing.Icon.ExtractAssociatedIcon(p_fsnItem.LastSource)); else*/ { string strIconPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) + strExtension; File.CreateText(strIconPath).Close(); ImageList.Images.Add(strExtension, System.Drawing.Icon.ExtractAssociatedIcon(strIconPath)); File.Delete(strIconPath); } } p_fsnItem.ImageKey = strExtension; p_fsnItem.SelectedImageKey = strExtension; } }
/// <summary> /// Sets the image of the given node. /// </summary> /// <param name="p_fsnItem">The node whose image shuld be set.</param> protected override void SetNodeImage(FileSystemTreeNode p_fsnItem) { if (p_fsnItem.IsDrive) { p_fsnItem.ImageKey = "drive"; p_fsnItem.SelectedImageKey = "drive"; } else base.SetNodeImage(p_fsnItem); }
/// <summary> /// This adds a file/folder to the source file structure. /// </summary> /// <param name="p_tndRoot">The node to which to add the file/folder.</param> /// <param name="p_strFile">The path to add to the source file structure.</param> private void addSourceFile(FileSystemTreeNode p_tndRoot, string p_strFile) { if (!p_strFile.StartsWith(Archive.ARCHIVE_PREFIX) && !p_strFile.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { FileSystemInfo fsiInfo; if (Directory.Exists(p_strFile)) { fsiInfo = new DirectoryInfo(p_strFile); } else if (File.Exists(p_strFile)) { fsiInfo = new FileInfo(p_strFile); } else { return; } if ((fsiInfo.Attributes & FileAttributes.System) > 0) { return; } } FileSystemTreeNode tndFile; var tncSiblings = (p_tndRoot == null) ? tvwSource.Nodes : p_tndRoot.Nodes; if (tncSiblings.ContainsKey(p_strFile)) { tndFile = (FileSystemTreeNode) tncSiblings[p_strFile]; } else { tndFile = new FileSystemTreeNode(Path.GetFileName(p_strFile), p_strFile); tndFile.Name = p_strFile; tncSiblings.Add(tndFile); } if (tndFile.IsDirectory) { tndFile.ImageKey = "folder"; tndFile.SelectedImageKey = "folder"; if ((p_tndRoot == null) || (p_tndRoot.IsExpanded)) { tndFile.Sources[p_strFile].IsLoaded = true; if (p_strFile.StartsWith(Archive.ARCHIVE_PREFIX)) { var kvpPath = Archive.ParseArchivePath(p_strFile); var arcArchive = new Archive(kvpPath.Key); var strFolders = arcArchive.GetDirectories(kvpPath.Value); foreach (var folder in strFolders) { addSourceFile(tndFile, Archive.GenerateArchivePath(kvpPath.Key, folder)); } var strFiles = arcArchive.GetFiles(kvpPath.Value); foreach (var file in strFiles) { addSourceFile(tndFile, Archive.GenerateArchivePath(kvpPath.Key, file)); } } else if (!p_strFile.StartsWith(FileSystemTreeNode.NEW_PREFIX)) { var strFolders = Directory.GetDirectories(p_strFile); foreach (var folder in strFolders) { addSourceFile(tndFile, folder); } var strFiles = Directory.GetFiles(p_strFile); foreach (var file in strFiles) { addSourceFile(tndFile, file); } } } } else { tndFile.Sources[p_strFile].IsLoaded = true; var strExtension = Path.GetExtension(p_strFile).ToLowerInvariant(); if (!imlIcons.Images.ContainsKey(strExtension)) { var strIconPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) + strExtension; File.CreateText(strIconPath).Close(); imlIcons.Images.Add(strExtension, Icon.ExtractAssociatedIcon(strIconPath)); File.Delete(strIconPath); } tndFile.ImageKey = strExtension; tndFile.SelectedImageKey = strExtension; if (tndFile.IsArchive) { var arcArchive = new Archive(p_strFile); var strFolders = arcArchive.GetDirectories("/"); foreach (var folder in strFolders) { addSourceFile(tndFile, Archive.GenerateArchivePath(p_strFile, folder)); } var strFiles = arcArchive.GetFiles("/"); foreach (var file in strFiles) { addSourceFile(tndFile, Archive.GenerateArchivePath(p_strFile, file)); } } } }
private FileSystemTreeNode() { _name = null; _children = new Dictionary<string, FileSystemTreeNode>(); _parent = null; }