How to create an application that lists folders containing the most disk space?
-
Hi All Does anyone have an answer to this? im using C# Visual studio 2008? So far I was thinking just loop through the first root folder, compare the length of the first file with the rest, and if the first files length is smaller then swap the bigger one with it, so at the end all the biggest files will be at the top.. DirectoryInfo dir = new DirectoryInfo(@"C:\"); foreach (FileInfo file in dir.GetFiles()) { Console.WriteLine(file.Name); foreach (FileInfo file2 in dir.GetFiles()) { if (file.Length < file2.Length) file2.MoveTo(file.ToString()); } } }
-
Hi All Does anyone have an answer to this? im using C# Visual studio 2008? So far I was thinking just loop through the first root folder, compare the length of the first file with the rest, and if the first files length is smaller then swap the bigger one with it, so at the end all the biggest files will be at the top.. DirectoryInfo dir = new DirectoryInfo(@"C:\"); foreach (FileInfo file in dir.GetFiles()) { Console.WriteLine(file.Name); foreach (FileInfo file2 in dir.GetFiles()) { if (file.Length < file2.Length) file2.MoveTo(file.ToString()); } } }
You can use LINQ to do this easily. http://dotnetperls.com/sort-file-size
Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
-
Hi All Does anyone have an answer to this? im using C# Visual studio 2008? So far I was thinking just loop through the first root folder, compare the length of the first file with the rest, and if the first files length is smaller then swap the bigger one with it, so at the end all the biggest files will be at the top.. DirectoryInfo dir = new DirectoryInfo(@"C:\"); foreach (FileInfo file in dir.GetFiles()) { Console.WriteLine(file.Name); foreach (FileInfo file2 in dir.GetFiles()) { if (file.Length < file2.Length) file2.MoveTo(file.ToString()); } } }
Luke Perrin wrote:
file2.MoveTo(file.ToString());
do you really want to move files around????? and do you think you can get a folder size without summing file sizes? :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi All Does anyone have an answer to this? im using C# Visual studio 2008? So far I was thinking just loop through the first root folder, compare the length of the first file with the rest, and if the first files length is smaller then swap the bigger one with it, so at the end all the biggest files will be at the top.. DirectoryInfo dir = new DirectoryInfo(@"C:\"); foreach (FileInfo file in dir.GetFiles()) { Console.WriteLine(file.Name); foreach (FileInfo file2 in dir.GetFiles()) { if (file.Length < file2.Length) file2.MoveTo(file.ToString()); } } }
You can use the code below, but it may need some performance improvements
List<FileInfo> fi = new List<FileInfo>(); ListBox l = new ListBox(); private void Form1\_Load(object sender, EventArgs e) { } private void GetSubFoldersAndFiles(string path) { foreach (string d in Directory.GetDirectories(path)) { try { GetSubFoldersAndFiles(d); } catch (Exception) { } } foreach (string f in Directory.GetFiles(path)) { fi.Add(new FileInfo(f)); } } private void Form1\_Shown(object sender, EventArgs e) { l.Dock = DockStyle.Fill; this.Controls.Add(l); foreach (DriveInfo di in DriveInfo.GetDrives()) { if (di.IsReady) { GetSubFoldersAndFiles(di.RootDirectory.FullName); } } var a = (from s in fi orderby s.Length descending select s); l.DataSource = a.ToList(); }