0
在下面的WMI查詢中,如果proptery爲空,那麼我的代碼錯誤。當屬性不存在時,如何讓代碼返回「」?例如queryObj [「幫助電話」]爲空,所以我的代碼出錯了,但我希望它繼續。WMI查詢空屬性
ManagementObjectSearcher searcherSoftware = new ManagementObjectSearcher("root\\CIMV2", "Select * from Win32_Product");
foreach (ManagementObject queryObj in searcherSoftware.Get())
{
ItemsUnderControlObject TemporarySoftware = new ItemsUnderControlObject();
TemporarySoftware.sType = "Software";
TemporarySoftware.sAssignmentType = queryObj["AssignmentType"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"'); //http://stackoverflow.com/questions/22583873/get-names-from-string-values/22583919?noredirect=1#22583919
TemporarySoftware.sCaption = queryObj["Caption"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
TemporarySoftware.sDescription = queryObj["Description"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
TemporarySoftware.sHelpLink = queryObj["HelpLink"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
TemporarySoftware.sHelpTelephone = queryObj["Help Telephone"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
}
線對象的PropertyValue = queryObj將[PropertyName];仍然跳轉到catch語句。爲什麼我不知道。前述描述是「未找到」,就好像該屬性完全不存在一樣。 – user1438082 2014-12-06 14:29:03
明白了 - 只要在代碼中加入try catch,並返回「」,如果它進入catch語句。 – user1438082 2014-12-06 14:45:43