C#擷取硬碟資訊

來源:互聯網
上載者:User
 1     /// <summary>
2 /// 硬碟
3 /// </summary>
4 public class VAV_MDDFM_HD
5 {
6 private static StringBuilder strBu = new StringBuilder();
7
8 /// <summary>
9 /// 擷取硬碟空間大小
10 /// </summary>
11 /// <param name="Drive">指定盤符,預設為ALL</param>
12 public static string Get_HD_Space(string Drive)
13 {
14 Dictionary<string, object> dicts = Get_HD_Space();
15 if (Drive.Trim().Length <= 0)
16 return strBu.ToString();
17 object o = null;
18 foreach (string key in dicts.Keys)
19 {
20
21 if (dicts.ContainsKey(Drive.ToLower()))
22 {
23 o = dicts[Drive.ToLower()];
24 break;
25 }
26
27 Console.WriteLine(dicts[key]);
28 }
29 return o.ToString();
30 }
31 /// <summary>
32 /// 列印硬碟空間大小
33 /// </summary>
34 /// <param name="Drive">指定盤符,預設為ALL</param>
35 public static void Print_HD_Space(string Drive)
36 {
37 Console.WriteLine(Get_HD_Space(Drive));
38 }
39
40 /// <summary>
41 /// 擷取硬碟空間大小
42 /// </summary>
43 public static Dictionary<string, object> Get_HD_Space()
44 {
45 DriveInfo[] allDrives = DriveInfo.GetDrives();
46
47 Dictionary<string, object> dict = new Dictionary<string, object>();
48
49 StringBuilder sb = new StringBuilder();
50
51 foreach (DriveInfo d in allDrives)
52 {
53 sb = new StringBuilder();
54 //Console.WriteLine("Drive {0}", d.Name);
55 sb.Append(string.Format(" Drive {0}", d.Name.ToLower()));
56 //Console.WriteLine(" File type: {0}", d.DriveType);
57 sb.Append(string.Format(" File type: {0}", d.DriveType));
58 if (d.IsReady == true)
59 {
60 sb.Append(string.Format("Volume label: {0}{1}", d.VolumeLabel, "\r\n"));
61 sb.Append(string.Format("File system: {0}{1}", d.DriveFormat, "\r\n"));
62 sb.Append(string.Format("Available space to current user:{0, 15} GB{1}", Utility.ConvertBytes(d.AvailableFreeSpace.ToString(), 3), "\r\n"));
63 sb.Append(string.Format("Total available space: {0, 15} GB{1}", Utility.ConvertBytes(d.TotalFreeSpace.ToString(), 3), "\r\n"));
64 sb.Append(string.Format("Total size of drive: {0, 15} GB{1}", Utility.ConvertBytes(d.TotalSize.ToString(), 3), "\r\n"));
65 }
66
67 strBu.Append(sb + "\r\n");
68
69 dict.Add(d.Name, sb);
70 }
71 return dict;
72 }
73 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.