/// <summary> /// Find most fragmented files and group by fragment count. /// </summary> /// <remarks> /// Requires RetrieveMode.Streams and RetrieveMode.Fragments /// </remarks> static void AnalyzeFragmentation(IEnumerable <INode> nodes, DriveInfo driveInfo) { //Fragmentation Example IDictionary <UInt32, List <INode> > fragmentsAggregate = Algorithms.AggregateByFragments(nodes, 2); List <UInt32> fragments = new List <UInt32>(fragmentsAggregate.Keys); fragments.Sort(); fragments.Reverse(); //make the most fragmented ones appear first string targetFile = Path.Combine(driveInfo.Name, "fragmentation.txt"); using (StreamWriter fs = new StreamWriter("c:\\fragmentation.txt")) { foreach (UInt32 fragment in fragments) { List <INode> fragmentedNodes = fragmentsAggregate[fragment]; fs.WriteLine("-----------------------------------------"); fs.WriteLine("FRAGMENTS: {0}", fragment); fs.WriteLine("-----------------------------------------"); foreach (INode node in fragmentedNodes) { fs.WriteLine( string.Format( "Index {0}, {1}, {2}, size {3}, path {4}, lastModification {5}", node.NodeIndex, (node.Attributes & Attributes.Directory) != 0 ? "Dir" : "File", node.Name, node.Size, node.FullName, node.LastChangeTime.ToLocalTime() ) ); } } } Console.WriteLine("Fragmentation Report has been saved to {0}", targetFile); }