C#获取硬盘信息
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 }