Get free disk space

前端 未结 13 1577
孤街浪徒
孤街浪徒 2020-12-02 13:04

Given each of the inputs below, I\'d like to get free space on that location. Something like

long GetFreeSpace(string path)

Inputs:

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 13:25

    Check this out (this is a working solution for me)

    public long AvailableFreeSpace()
    {
        long longAvailableFreeSpace = 0;
        try{
            DriveInfo[] arrayOfDrives = DriveInfo.GetDrives();
            foreach (var d in arrayOfDrives)
            {
                Console.WriteLine("Drive {0}", d.Name);
                Console.WriteLine("  Drive type: {0}", d.DriveType);
                if (d.IsReady == true && d.Name == "/data")
                {
                    Console.WriteLine("Volume label: {0}", d.VolumeLabel);
                    Console.WriteLine("File system: {0}", d.DriveFormat);
                    Console.WriteLine("AvailableFreeSpace for current user:{0, 15} bytes",d.AvailableFreeSpace);
                    Console.WriteLine("TotalFreeSpace {0, 15} bytes",d.TotalFreeSpace);
                    Console.WriteLine("Total size of drive: {0, 15} bytes \n",d.TotalSize);
                    }
                    longAvailableFreeSpaceInMB = d.TotalFreeSpace;
            }
        }
        catch(Exception ex){
            ServiceLocator.GetInsightsProvider()?.LogError(ex);
        }
        return longAvailableFreeSpace;
    }
    

提交回复
热议问题