Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to create an application that lists folders containing the most disk space?

How to create an application that lists folders containing the most disk space?

Scheduled Pinned Locked Moved C#
csharpvisual-studiojsontutorialquestion
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luke Perrin
    wrote on last edited by
    #1

    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()); } } }

    M L T 3 Replies Last reply
    0
    • L Luke Perrin

      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()); } } }

      M Offline
      M Offline
      MarkB777
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • L Luke Perrin

        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()); } } }

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.


        1 Reply Last reply
        0
        • L Luke Perrin

          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()); } } }

          T Offline
          T Offline
          Tamer Oz
          wrote on last edited by
          #4

          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();
              }
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups