// <summary> // Method for copying "virtual" tree branch; called recursively // </summary> private void CopyVirtualBranch(MyTreeNode start, FolderInventoryNode root) { start.virtualNode = root; if (root.Items.Count > 0) { IOrderedEnumerable <FolderInventoryNode> items; if (_current_sort_order == SortOrder.Alpha) { items = (IOrderedEnumerable <FolderInventoryNode>)root.Items.OrderBy(t => t.Name); } else { items = (IOrderedEnumerable <FolderInventoryNode>)root.Items.OrderByDescending(t => t.TotalWeight); } foreach (FolderInventoryNode item in items) { MyTreeNode treenode = new MyTreeNode(item.ToString(_active_measure_unit, _LocRM)); treenode.BackColor = item.GetColorByDirInfo(treenode.ForeColor); start.Nodes.Add(treenode); CopyVirtualBranch(treenode, item); } } }
/// <summary> /// Recursive method to save directory to file /// </summary> /// <param name="root"></param> /// <param name="parent"></param> /// <param name="file"></param> private void WriteInventoryPath(FolderInventoryNode root, string parent) { _file.WriteLine(root.ToFileString(parent)); foreach (FolderInventoryNode node in root.Items) { WriteInventoryPath(node, (parent == "" ? "" : parent + @"\") + root.Name); } }
// <summary> // Method for rebuilding TreeView by already built "virtual" Tree // </summary> public void RepopulateTreeView() { FolderInventoryNode internal_root = _folder_inventory.Root; if (internal_root == null) { return; } string path = internal_root.Name; MyTreeNode start = new MyTreeNode(path); tvInventory.Nodes.Clear(); tvInventory.Nodes.Add(start); CopyVirtualBranch(start, internal_root); start.Text = internal_root.ToString(_active_measure_unit, _LocRM); }
static private string FullPath(FolderInventoryNode node) { string path = ""; while (node != null) { if (path.Length > 0) { path = node.Name + @"\" + path; } else { path = node.Name; } node = node.Parent; } if (path.Length == 2) { path += @"\"; } return(path); }
// <summary> // Scans directory and populates virtual tree; called recursively // </summary> private void PopulateDirectoryBranch(bool is_top, string path, FolderInventoryNode root) { DirectoryInfo di = new DirectoryInfo(path); FileAttributes fa = File.GetAttributes(path); if ((fa & FileAttributes.ReparsePoint) > 0) { root.Result = DirOpenResult.E_HARDLINK; root.TotalWeight = 0; LogFolder(_LocRM.GetString("LOG_HardLink") + ": " + path); return; } IEnumerable <String> dirs = null; try { dirs = di.EnumerateDirectories().Select(t => t.Name); root.InitializeDirInfo(path); } catch (UnauthorizedAccessException e) { root.Result = DirOpenResult.E_ACCESSDENIED; root.TotalWeight = 0; LogFolder(_LocRM.GetString("LOG_AccessDenied") + ": " + path); } int i = 0; if (dirs != null) { foreach (string dir in dirs) { if (CancellationPending) { break; } i++; if (UpdateDirectoryLabelHandler != null) { UpdateDirectoryLabelHandler(path); } FolderInventoryNode item = new FolderInventoryNode(root); Application.DoEvents(); item.Name = dir; string full_path = path + "\\" + dir; root.Items.Add(item); try { PopulateDirectoryBranch(false, full_path, item); } catch (PathTooLongException e) { root.Result = DirOpenResult.E_OTHER; root.TotalWeight = 0; LogFolder(_LocRM.GetString("LOG_PathTooLong") + ": " + path); } if (is_top) { if (ReportProgressHandler != null) { ReportProgressHandler(i * 100 / dirs.Count()); } } root.SubWeight += item.TotalWeight; } root.TotalWeight = root.OwnWeight + root.SubWeight; } }
public FolderInventoryNode(FolderInventoryNode parent) : this() { _parent = parent; }
public bool OpenInventory(string filename) { string line; string full_name; int counter = 0; Regex rx = new Regex(@"(\d+)\s+(\d+)\s+(.+)", RegexOptions.Compiled); StreamReader file = new System.IO.StreamReader(filename, Encoding.GetEncoding(1251)); Match match; _internal_root = new FolderInventoryNode(); FolderInventoryNode root = _internal_root; bool first_string_processing = true; while ((line = file.ReadLine()) != null) { //_log.Add(line); match = rx.Match(line); FolderInventoryNode item = new FolderInventoryNode(root); if (match.Success) { GroupCollection groups = match.Groups; item.TotalWeight = Convert.ToInt64(groups[1].ToString()); item.OwnWeight = Convert.ToInt64(groups[2].ToString()); full_name = groups[3].ToString(); if (first_string_processing) { root.Name = full_name; //dir.FullName; root.TotalWeight = item.TotalWeight; root.OwnWeight = item.OwnWeight; first_string_processing = false; } else { item.Name = GetFolderName(full_name); string parent_path = GetParentPath(full_name); string fp = FullPath(root); if (fp != parent_path) { // lbLogs.Items.Add("Full path for root (" + fp + ") not equal to parent path:" + parent_path); } while (root != null && fp != parent_path) { root = root.Parent; if (root != null) { fp = FullPath(root); } } if (root == null) { break; } item.Parent = root; root.Items.Add(item); root = item; } } counter++; } file.Close(); return(true); }
public void InitializeInventory(string path) { _path = path; _internal_root = new FolderInventoryNode(); _internal_root.Name = _path; }
public MyTreeNode(string str) : base(str) { virtualNode = null; }
public bool OpenInventory(string filename) { string line; string full_name; int counter = 0; Regex rx = new Regex(@"(\d+)\s+(\d+)\s+(.+)", RegexOptions.Compiled); StreamReader file = new System.IO.StreamReader(filename, Encoding.GetEncoding(1251)); Match match; _internal_root = new FolderInventoryNode(); FolderInventoryNode root = _internal_root; bool first_string_processing = true; while ((line = file.ReadLine()) != null) { //_log.Add(line); match = rx.Match(line); FolderInventoryNode item = new FolderInventoryNode(root); if (match.Success) { GroupCollection groups = match.Groups; item.TotalWeight = Convert.ToInt64(groups[1].ToString()); item.OwnWeight = Convert.ToInt64(groups[2].ToString()); full_name = groups[3].ToString(); if (first_string_processing) { root.Name = full_name; //dir.FullName; root.TotalWeight = item.TotalWeight; root.OwnWeight = item.OwnWeight; first_string_processing = false; } else { item.Name = GetFolderName(full_name); string parent_path = GetParentPath(full_name); string fp = FullPath(root); if (fp != parent_path) { // lbLogs.Items.Add("Full path for root (" + fp + ") not equal to parent path:" + parent_path); } while (root != null && fp != parent_path) { root = root.Parent; if (root != null) { fp = FullPath(root); } } if (root == null) { break; } item.Parent = root; root.Items.Add(item); root = item; } } counter++; } file.Close(); return true; }
// <summary> // Method for copying "virtual" tree branch; called recursively // </summary> private void CopyVirtualBranch(MyTreeNode start, FolderInventoryNode root) { start.virtualNode = root; if (root.Items.Count > 0) { IOrderedEnumerable<FolderInventoryNode> items; if (_current_sort_order == SortOrder.Alpha) { items = (IOrderedEnumerable<FolderInventoryNode>)root.Items.OrderBy(t => t.Name); } else { items = (IOrderedEnumerable<FolderInventoryNode>)root.Items.OrderByDescending(t => t.TotalWeight); } foreach (FolderInventoryNode item in items) { MyTreeNode treenode = new MyTreeNode(item.ToString(_active_measure_unit, _LocRM)); treenode.BackColor = item.GetColorByDirInfo(treenode.ForeColor); start.Nodes.Add(treenode); CopyVirtualBranch(treenode, item); } } }
public FolderInventoryNode() { _items = new List <FolderInventoryNode>(); _parent = null; }
public FolderInventoryNode() { _items = new List<FolderInventoryNode>(); _parent = null; }
/// <summary> /// Recursive method to save directory to file /// </summary> /// <param name="root"></param> /// <param name="parent"></param> /// <param name="file"></param> private void WriteInventoryPath(FolderInventoryNode root, string parent) { _file.WriteLine(root.ToFileString(parent)); foreach (FolderInventoryNode node in root.Items) { WriteInventoryPath(node, (parent == "" ? "" : parent+@"\") +root.Name); } }
// <summary> // Scans directory and populates virtual tree; called recursively // </summary> private void PopulateDirectoryBranch(bool is_top, string path, FolderInventoryNode root) { DirectoryInfo di = new DirectoryInfo(path); FileAttributes fa = File.GetAttributes(path); if ((fa & FileAttributes.ReparsePoint) > 0) { root.Result = DirOpenResult.E_HARDLINK; root.TotalWeight = 0; LogFolder(_LocRM.GetString("LOG_HardLink") + ": " + path); return; } IEnumerable<String> dirs = null; try { dirs = di.EnumerateDirectories().Select(t => t.Name); root.InitializeDirInfo(path); } catch (UnauthorizedAccessException e) { root.Result = DirOpenResult.E_ACCESSDENIED; root.TotalWeight = 0; LogFolder(_LocRM.GetString("LOG_AccessDenied") + ": " + path); } int i = 0; if (dirs != null) { foreach (string dir in dirs) { if (CancellationPending) { break; } i++; if (UpdateDirectoryLabelHandler != null) { UpdateDirectoryLabelHandler(path); } FolderInventoryNode item = new FolderInventoryNode(root); Application.DoEvents(); item.Name = dir; string full_path = path + "\\" + dir; root.Items.Add(item); PopulateDirectoryBranch(false, full_path, item); if (is_top) { if (ReportProgressHandler != null) { ReportProgressHandler(i * 100 / dirs.Count()); } } root.SubWeight += item.TotalWeight; } root.TotalWeight = root.OwnWeight + root.SubWeight; } }
private static string FullPath(FolderInventoryNode node) { string path = ""; while (node != null) { if (path.Length > 0) { path = node.Name + @"\" + path; } else { path = node.Name; } node = node.Parent; } if (path.Length == 2) { path += @"\"; } return path; }