CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Drive Space!

  1. #1
    Join Date
    Jul 2005
    Posts
    22

    Drive Space!

    does anyone know what i would use to get the free space on my local hard drive.

    I am looking for something that I can pass the drive letter into.

    something that would work like this... (i know this is not actuall code just givving an example of how i would expect it to function)



    intFreeSpace = localmachine.drivespace.freespace(driveletter);

    Thanks in Advance,

    David

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Drive Space!

    You could do something like this:
    Code:
    [STAThread]
    static void Main(string[] args)
    {
        Console.WriteLine("Free space {0}\n", GetDiskSpace("c:"));
    }
    
    static UInt64 GetDiskSpace(string drive)
    {
        // make it upper
        drive = drive.ToUpper();
    
        // enumerate disks
        ManagementClass pDiskClass = new ManagementClass("Win32_LogicalDisk");
        ManagementObjectCollection pDisks = pDiskClass.GetInstances();
    
        foreach (ManagementObject pDisk in pDisks)
            if (drive.CompareTo((string)pDisk["name"]) == 0)
                return (UInt64) pDisk["FreeSpace"];
    
        // disk not found
        return 0;
    }
    - petter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured