首页 > Windows开发 > 详细

C#获取磁盘空间

时间:2021-06-02 15:27:32      阅读:14      评论:0      收藏:0      [点我收藏+]

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

技术分享图片

代码:

 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#获取磁盘空间

原文:https://www.cnblogs.com/pgblogs/p/14840004.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!