我有ManagementObject
的集合。管理对象的属性是不同的类型:int,string,array,DateTime等。
我想将属性值读入适当的变量类型。
ManagementScope manScope = new ManagementScope(@"\\.\root\virtualization\v2");
manScope.Connect();
ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem");
ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj);
ManagementObjectCollection vmCollection = vmSearcher.Get();
foreach (ManagementObject vm in vmCollection) {
**//Fine so far. This is not a question about iteration.**
//Now I want to so something programmatic with the values.
//If it is a string, this is fine:
string myString = vm["Description"].ToString();
//If it is an integer, I can go like this:
int myInt =int.Parse(vm["EnabledState"].ToString());
//But surely c# has a better way than that? Turn it into a string and then an int?
//If it is DateTime, I have no idea what to do - DateTime.Parse() doesn't work:
//DateTime myDate = DateTime.Parse(vm["LastSuccessfulBackupTime"].ToString());
}
我已经阅读了文档(其中没有一个例子)。 我使用了WMI代码创建器,它对此没有任何帮助,因为它只是将所有东西都放到控制台中:Console.WriteLine(" InstallDate:{0}",queryObj [&#34] ; InstallDate"]);
答案 0 :(得分:0)
这是非常不寻常的要求,但仍然 - 我建议通过课程详细信息Msvm_computersystem并创建具有相同数据类型的变量。 获得集合中的数据后,可以尝试将其解析为适当的变量。