/// <summary> /// Remove a WMI instance. /// </summary> /// <param name="obj">Object to be removed.</param> public void RemoveInstance(object obj) { try { WindowsImpersonationContext impersonatedUser = WindowsIdentity.GetCurrent().Impersonate(); string className = TypeHelper.GetClassName(obj); WMISearchKey key = TypeHelper.GetSearchKey(obj); string query = String.Format("SELECT * FROM {0} WHERE {1} = '{2}'", className, key.Name, key.Value); ManagementObjectSearcher searcher; searcher = new ManagementObjectSearcher(Scope, query); foreach (ManagementObject m in searcher.Get()) { m.Delete(); } } catch (Exception ex) { throw ex; } }
/// <summary> /// Modifies an existing instance. /// </summary> /// <param name="obj">Object to be updated. ORMi will search the property with the SearchKey attribute. That value is going to be used to make the update.</param> public void UpdateInstance(object obj) { try { WindowsImpersonationContext impersonatedUser = WindowsIdentity.GetCurrent().Impersonate(); string className = TypeHelper.GetClassName(obj); WMISearchKey key = TypeHelper.GetSearchKey(obj); if (key.Value != null) { string query = String.Format("SELECT * FROM {0} WHERE {1} = '{2}'", TypeHelper.GetClassName(obj), key.Name, key.Value); ManagementObjectSearcher searcher; searcher = new ManagementObjectSearcher(Scope, query); ManagementObjectCollection col = searcher.Get(); foreach (ManagementObject m in searcher.Get()) { foreach (PropertyInfo p in obj.GetType().GetProperties()) { WMIIgnore ignoreProp = p.GetCustomAttribute <WMIIgnore>(); if (ignoreProp == null) { WMIProperty propAtt = p.GetCustomAttribute <WMIProperty>(); if (propAtt != null) { m[propAtt.Name] = p.GetValue(obj).GetType() == typeof(DateTime) ? ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(p.GetValue(obj))) : p.GetValue(obj); } else { m[p.Name] = p.GetValue(obj).GetType() == typeof(DateTime) ? ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(p.GetValue(obj))) : p.GetValue(obj); } } } m.Put(); } } } catch (Exception ex) { throw ex; } }