查询当前电脑的磁盘空间,并给于提示,是一个完善软件的标配.
1.首先查询系统的盘符
SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
foreach (ManagementObject disk in searcher.Get())
{
zcmb.Items.Add(disk["Name"].ToString());
}
if (zcmb.Items.Count > 0) { zcmb.SelectedIndex = 0; }
2.查询每隔盘符的空间情况
private void zcmb_SelectedIndexChanged(object sender, EventArgs e)
{
string name = zcmb.Text;
if (string.IsNullOrEmpty(name)) { return; }
DriveInfo dinfo = new DriveInfo(name);
ztol.Text = "总容量:" + FormatFileSize(dinfo.TotalSize);
zhas.Text = "剩余容量:" + FormatFileSize(dinfo.TotalFreeSpace);
}