代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Management;
using System.IO;
namespace demo5
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
String DeviceName;
Console.WriteLine("请输入你要查询的分区代码:");
DeviceName = Console.ReadLine();
SelectQuery selectQuery = new SelectQuery(string.Format("select * from win32_logicaldisk WHERE DeviceID ='{0}'",DeviceName.Substring(0,2)));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
ManagementObjectCollection diskcollection = searcher.Get();
if (diskcollection != null && diskcollection.Count > 0)
{
HardDiskPartition harddisk = null;
harddisk = new HardDiskPartition();
foreach (ManagementObject disk in searcher.Get()) //ManagementObject mobj in wmifind.Get()
{
harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024 * 1024);
harddisk.SumSpace = Convert.ToDouble(disk["Size"]) / (1024 * 1024 * 1024);
harddisk.PartitionName = disk["DeviceID"].ToString(); // Console.WriteLine(("{0} 总空间:{1} GB,剩余:{2} GB", harddisk.PartitionName,harddisk.SumSpace, harddisk.FreeSpace);
Console.Write("{0}总空间:{1} GB,剩余:{2} GB", harddisk.PartitionName, Math.Round(harddisk.SumSpace), Math.Round(harddisk.FreeSpace)); Console.ReadKey();
}
}
//WqlObjectQuery wmiquery = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'");
}
}
}
在 String DeviceName;
Console.WriteLine("请输入你要查询的分区代码:");
DeviceName = Console.ReadLine();
SelectQuery selectQuery = new SelectQuery(string.Format("select * from win32_logicaldisk WHERE DeviceID ='{0}'",DeviceName.Substring(0,2)));这一步就给我提示这个问题,难道是这个查询语句里的条件值必须得是方法中传参,不能这样子直接赋值的么
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Management;
using System.IO;
namespace demo5
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
String DeviceName;
Console.WriteLine("请输入你要查询的分区代码:");
DeviceName = Console.ReadLine();
SelectQuery selectQuery = new SelectQuery(string.Format("select * from win32_logicaldisk WHERE DeviceID ='{0}'",DeviceName.Substring(0,2)));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
ManagementObjectCollection diskcollection = searcher.Get();
if (diskcollection != null && diskcollection.Count > 0)
{
HardDiskPartition harddisk = null;
harddisk = new HardDiskPartition();
foreach (ManagementObject disk in searcher.Get()) //ManagementObject mobj in wmifind.Get()
{
harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024 * 1024);
harddisk.SumSpace = Convert.ToDouble(disk["Size"]) / (1024 * 1024 * 1024);
harddisk.PartitionName = disk["DeviceID"].ToString(); // Console.WriteLine(("{0} 总空间:{1} GB,剩余:{2} GB", harddisk.PartitionName,harddisk.SumSpace, harddisk.FreeSpace);
Console.Write("{0}总空间:{1} GB,剩余:{2} GB", harddisk.PartitionName, Math.Round(harddisk.SumSpace), Math.Round(harddisk.FreeSpace)); Console.ReadKey();
}
}
//WqlObjectQuery wmiquery = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'");
}
}
}
在 String DeviceName;
Console.WriteLine("请输入你要查询的分区代码:");
DeviceName = Console.ReadLine();
SelectQuery selectQuery = new SelectQuery(string.Format("select * from win32_logicaldisk WHERE DeviceID ='{0}'",DeviceName.Substring(0,2)));这一步就给我提示这个问题,难道是这个查询语句里的条件值必须得是方法中传参,不能这样子直接赋值的么
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货