开发者

How to calculate free disk space?

开发者 https://www.devze.com 2023-01-07 00:00 出处:网络
I\'m working on an installer project where I need to extract files to the disk. How can I calculate/find the disk space available on 开发者_运维知识库hard disk using c#?http://msdn.microsoft.com/en-us

I'm working on an installer project where I need to extract files to the disk. How can I calculate/find the disk space available on 开发者_运维知识库hard disk using c#?


http://msdn.microsoft.com/en-us/library/system.io.driveinfo.totalfreespace.aspx

Copied from the link

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        DriveInfo[] allDrives = DriveInfo.GetDrives();

        foreach (DriveInfo d in allDrives)
        {
            Console.WriteLine("Drive {0}", d.Name);
            Console.WriteLine("  File type: {0}", d.DriveType);
            if (d.IsReady == true)
            {
                Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
                Console.WriteLine("  File system: {0}", d.DriveFormat);
                Console.WriteLine(
                    "  Available space to current user:{0, 15} bytes", 
                    d.AvailableFreeSpace);

                Console.WriteLine(
                    "  Total available space:          {0, 15} bytes",
                    d.TotalFreeSpace);

                Console.WriteLine(
                    "  Total size of drive:            {0, 15} bytes ",
                    d.TotalSize);
            }
        }
    }
}


Use the System.IO.DriveInfo class. There are two options available...one that takes disk quotas into account:

var drive = new DriveInfo("c");
long freeSpaceInBytes = drive.AvailableFreeSpace;

and one that simply provides the total free space:

var drive = new DriveInfo("c");
long freeSpaceInBytes = drive.TotalFreeSpace;


You can also use WMI.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx

using System;
using System.Management;

class Test
{
    static void Main()
    {
        var moCollection = new ManagementClass("Win32_LogicalDisk").GetInstances();

        foreach (var mo in moCollection)
        {
            if (mo["DeviceID"] != null && mo["DriveType"] != null && mo["Size"] != null && mo["FreeSpace"] != null)
            {
                // DriveType 3 = "Local Disk"
                if (Convert.ToInt32(mo["DriveType"]) == 3)
                {
                    Console.WriteLine("Drive {0}", mo["DeviceID"]);
                    Console.WriteLine("Size {0} bytes", mo["Size"]);
                    Console.WriteLine("Free {0} bytes", mo["FreeSpace"]);
                }
            }
        }
    }
}


Do you need to care? Just write the files to the disk and handle the errors as needed - it's assumed that you'll have to implement rollback anyways in case something non space-related happens, so just treat "no disk space" as just another error that you need to rollback for.

Update: If you're coming here to downvote this correct answer, please leave a comment as to why.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号