Check available storage disk space with C#

This below code get all disk space with c# code




 

public void CheckAvailableFreeSpace()
{
foreach (DriveInfo d in allDrives)
                {
                    if (d.IsReady == true)
                    {
                        if (d.DriveType == DriveType.Fixed)
                        {                         

                            string content = string.Format("Time: {0} available space are {1} GB", DateTime.Now + 
" Server: " + serverName + " Volume: " + d.Name + d.VolumeLabel, convertSize(d.AvailableFreeSpace));
                           
                            double totalSpace = convertSize(d.AvailableFreeSpace);                            
                        }
                    }
                }
}

private double convertSize(double size)
        {
            //String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };

            String[] units = new String[] { "GB" };

            double mod = 1024.0;

            int i = 0;

            while (size >= mod)
            {
                size /= mod;
                i++;
            }
            return Math.Round(size);// + units[i];
        }
Reactions

Post a Comment

0 Comments