Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,822 answers

573 users

How to get free disk space in MB with C#

1 Answer

0 votes
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        private long GetTotalFreeSpace(string driveName)
        {
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                if (drive.IsReady && drive.Name == driveName)
                {
                    return drive.TotalFreeSpace;
                }
            }
            return -1;
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = string.Format("{0:n}", GetTotalFreeSpace("C:\\") / 1024.0 / 1024.0) + " MB Free";
        }
    }
}




/*
 * run:
 *
 * 512,198.52 MB Free
 * 
 */

 





answered Aug 20, 2023 by avibootz

Related questions

1 answer 34 views
34 views asked Aug 20, 2023 by avibootz
1 answer 31 views
2 answers 130 views
...