How to get the total free space in all drives using WinForms in C#

1 Answer

0 votes
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                {
                    textBox2.Text += drive.Name + "  " + string.Format("{0:n}", drive.TotalFreeSpace) + " bytes free" + Environment.NewLine;
                }
            }
        }


    }
}



/*
run:

C:\  457,667,014,656.00 bytes free
D:\  786,328,387,584.00 bytes free

*/

 



answered Jul 18, 2024 by avibootz
edited Jul 18, 2024 by avibootz
...