C#获取磁盘空间

做一个简单的测试界面,如下图:

C#获取磁盘空间

代码:

 1  private void button1_Click(object sender, EventArgs e)
 2         {
 3             string strRecordFilePath = "c:";
 4             string str = strRecordFilePath.Substring(0, 1).ToUpper();
 5             this.m_ChkDisk(str);
 6         }
 7 
 8 
 9         private void m_ChkDisk(string strDisk)
10         {
11             if (strDisk == "")
12                 return;
13             txtTotal.Text = GetDiskSpace(strDisk).ToString();
14             txtFreeSpace.Text = GetDiskFreeSpace(strDisk).ToString();
15             int a, b, c;
16             a = Convert.ToInt32(txtTotal.Text);
17             b = Convert.ToInt32(txtFreeSpace.Text);
18             c = a - b;
19 
20             txtUse.Text = c.ToString();
21         }
22 
23         public static long GetDiskSpace(string str_HardDiskName)
24         {
25             long totalSize = new long();
26             str_HardDiskName = str_HardDiskName + ":\\";
27             System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
28             foreach (System.IO.DriveInfo drive in drives)
29             {
30                 if (drive.Name == str_HardDiskName)
31                 {
32                     totalSize = drive.TotalSize / (1024 * 1024 * 1024);
33                 }
34             }
35             return totalSize;
36         }
37  
38         public static long GetDiskFreeSpace(string str_HardDiskName)
39         {
40             long freeSpace = new long();
41             str_HardDiskName = str_HardDiskName + ":\\";
42             System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
43             foreach (System.IO.DriveInfo drive in drives)
44             {
45                 if (drive.Name == str_HardDiskName)
46                 {
47                     freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);
48                 }
49             }
50             return freeSpace;
51         }

 

C#获取磁盘空间

上一篇:Surface RT在Win10中安装UWP软件,证书签名【已解决】


下一篇:C#自定义控件(3)—PanelHead控件