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,038 questions

40,897 answers

573 users

How to get free and available and total disk space in bytes using kernel32.dll in C#

Learn & Practice SQL


41 views
asked Aug 19, 2023 by avibootz
edited Aug 19, 2023 by avibootz

1 Answer

0 votes
using System.Runtime.InteropServices;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
                                    out ulong lpFreeBytesAvailable,
                                    out ulong lpTotalNumberOfBytes,
                                    out ulong lpTotalNumberOfFreeBytes);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ulong freeBytesAvailable;
            ulong totalNumOfBytes;
            ulong totalNumOfFreeBytes;

            if (!GetDiskFreeSpaceEx("C:\\", out freeBytesAvailable, out totalNumOfBytes, out totalNumOfFreeBytes))
            {
                Console.Error.WriteLine("GetDiskFreeSpaceEx Error");
            }
            else
            {
                label1.Text = "";
                label1.Text += "Available bytes: " + string.Format("{0:n}", freeBytesAvailable) + Environment.NewLine;
                label1.Text += "Total # of bytes: " + string.Format("{0:n}", totalNumOfBytes) + Environment.NewLine;
                label1.Text += "Total free bytes: " + string.Format("{0:n}", totalNumOfFreeBytes) + Environment.NewLine;
            }
        }
    }
}



/*
 * run:
 *
 * Available bytes: 541,480,148,992.00
 * Total # of bytes: 999,559,262,208.00
 * Total free bytes: 541,480,148,992.00
 * 
 */

 





answered Aug 19, 2023 by avibootz

Related questions

1 answer 31 views
1 answer 37 views
37 views asked Aug 20, 2023 by avibootz
1 answer 35 views
35 views asked Aug 20, 2023 by avibootz
1 answer 38 views
...