private static List <FrnFilePath> GetFolderPath(UsnEntry[] folders, DriveInfo drive) { Dictionary <UInt64, FrnFilePath> pathDic = new Dictionary <ulong, FrnFilePath>(); pathDic.Add(ROOT_FILE_REFERENCE_NUMBER, new FrnFilePath(ROOT_FILE_REFERENCE_NUMBER, null, string.Empty, drive.Name.TrimEnd('\\'))); foreach (var folder in folders) { pathDic.Add(folder.FileReferenceNumber, new FrnFilePath(folder.FileReferenceNumber, folder.ParentFileReferenceNumber, folder.FileName)); } Stack <UInt64> treeWalkStack = new Stack <ulong>(); foreach (var key in pathDic.Keys) { treeWalkStack.Clear(); FrnFilePath currentValue = pathDic[key]; if (string.IsNullOrWhiteSpace(currentValue.Path) && currentValue.ParentFileReferenceNumber.HasValue && pathDic.ContainsKey(currentValue.ParentFileReferenceNumber.Value)) { FrnFilePath parentValue = pathDic[currentValue.ParentFileReferenceNumber.Value]; while (string.IsNullOrWhiteSpace(parentValue.Path) && parentValue.ParentFileReferenceNumber.HasValue && pathDic.ContainsKey(parentValue.ParentFileReferenceNumber.Value)) { currentValue = parentValue; if (currentValue.ParentFileReferenceNumber.HasValue && pathDic.ContainsKey(currentValue.ParentFileReferenceNumber.Value)) { treeWalkStack.Push(key); parentValue = pathDic[currentValue.ParentFileReferenceNumber.Value]; } else { parentValue = null; break; } } if (parentValue != null) { currentValue.Path = BuildPath(currentValue, parentValue); while (treeWalkStack.Count() > 0) { UInt64 walkedKey = treeWalkStack.Pop(); FrnFilePath walkedNode = pathDic[walkedKey]; FrnFilePath parentNode = pathDic[walkedNode.ParentFileReferenceNumber.Value]; walkedNode.Path = BuildPath(walkedNode, parentNode); } } } } var result = pathDic.Values.Where(p => !string.IsNullOrWhiteSpace(p.Path) && p.Path.StartsWith(drive.Name)).ToList(); return(result); }
private static string BuildPath(FrnFilePath currentNode, FrnFilePath parentNode) { return(string.Concat(new string[] { parentNode.Path, "\\", currentNode.FileName })); }