獲取本地硬盤的全部剩餘空間:json
主要應用到System.IO類庫的:Driveinfo、Directory,將model轉換成json須要用到Newtonsoft.Json.JsonConvert.SerializeObject方法spa
主要方法:code
public DiskList DisklistCon(string disk) { DiskList dl = new DiskList(); string[] diskcon = null; dl.msgList = new List<DiskCon>(); dl.msg = "成功!"; if (!string.IsNullOrEmpty(disk)) { DriveInfo di = new DriveInfo(disk); DiskCon dk = new DiskCon(); dk.DiskName = disk; dk.DiskLength = di.AvailableFreeSpace; dl.msgList.Add(dk); dl.count = 1; } else { try { diskcon = Directory.GetLogicalDrives(); } catch { } if (diskcon != null && diskcon.Count() > 0) { foreach (string diskname in diskcon) { DriveInfo di = new DriveInfo(diskname); DiskCon dk = new DiskCon(); dk.DiskName = diskname; dk.DiskLength = di.AvailableFreeSpace; dl.msgList.Add(dk); } dl.count = diskcon.Count(); } } return dl; }
Model方法:blog
public class DiskList { public string msg { get; set; } public List<DiskCon> msgList { get; set; } public int count { get; set; } } public class DiskCon { public string DiskName { get; set; } public long DiskLength { get; set; } public string DiskGb { get { return (DiskLength * 1.0 / 1024 / 1024 / 1024).ToString("0.00GB"); } } }