Finding out total and free disk space in .NET

前端 未结 9 1189
我在风中等你
我在风中等你 2021-01-04 03:08

I am trying to find a way to determine the total and available disk space in an arbitrary folder from a .NET app. By \"total disk space\" and \"available disk space\" in a f

9条回答
  •  北海茫月
    2021-01-04 03:41

    This may not be what you want, but I'm trying to help, and it has the bonus of slightly secure erasing the free space of your drive.

    public static string DriveSizeAvailable(string path)
    {
        long count = 0;
        byte toWrite = 1;
        try
        {
            using (StreamWriter writer = new StreamWriter(path))
            {
                while (true)
                {
                    writer.Write(toWrite);
                    count++;
                }
            }
        }
        catch (IOException)
        {                
        }
    
        return string.Format("There used to be {0} bytes available on drive {1}.", count, path);
    }
    
    public static string DriveSizeTotal(string path)
    {
        DeleteAllFiles(path);
        int sizeAvailable = GetAvailableSize(path);
        return string.Format("Drive {0} will hold a total of {1} bytes.", path, sizeAvailable);
    }
    

提交回复
热议问题