public void Add(string propertyName, object propertyValue, CimType propertyType) { if (propertyName == null) { throw new ArgumentNullException("propertyName"); } if (this.parent.GetType() == typeof(ManagementObject)) { throw new InvalidOperationException(); } int type = (int)propertyType; bool isArray = false; if ((propertyValue != null) && propertyValue.GetType().IsArray) { isArray = true; type |= 0x2000; } object pVal = PropertyData.MapValueToWmiValue(propertyValue, propertyType, isArray); int errorCode = this.parent.wbemObject.Put_(propertyName, 0, ref pVal, type); if (errorCode < 0) { if ((errorCode & 0xfffff000L) == 0x80041000L) { ManagementException.ThrowWithExtendedInfo((ManagementStatus)errorCode); } else { Marshal.ThrowExceptionForHR(errorCode); } } }
/// <summary> /// <para>Adds a new <see cref='System.Management.PropertyData'/> with the specified value and CIM type.</para> /// </summary> /// <param name='propertyName'>The name of the property.</param> /// <param name='propertyValue'>The value of the property (which can be null).</param> /// <param name='propertyType'>The CIM type of the property.</param> /// <remarks> /// <para> Properties can only be added to class definitions, not /// to instances. This method is only valid when invoked on a <see cref='System.Management.PropertyDataCollection'/> /// in /// a <see cref='System.Management.ManagementClass'/>.</para> /// </remarks> public void Add(string propertyName, object propertyValue, CimType propertyType) { if (null == propertyName) throw new ArgumentNullException(nameof(propertyName)); if (parent.GetType() == typeof(ManagementObject)) //can't add properties to instance throw new InvalidOperationException(); int wmiCimType = (int)propertyType; bool isArray = false; if ((null != propertyValue) && propertyValue.GetType().IsArray) { isArray = true; wmiCimType = (wmiCimType | (int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY); } object wmiValue = PropertyData.MapValueToWmiValue(propertyValue, propertyType, isArray); int status = parent.wbemObject.Put_(propertyName, 0, ref wmiValue, wmiCimType); if (status < 0) { if ((status & 0xfffff000) == 0x80041000) ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); else Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f()); } }
public virtual void Add(string propertyName, object propertyValue) { if (propertyValue != null) { if (this.parent.GetType() != typeof(ManagementObject)) { CimType cimType = CimType.None; bool flag = false; object wmiValue = PropertyData.MapValueToWmiValue(propertyValue, out flag, out cimType); int num = (int)cimType; if (flag) { num = num | 0x2000; } int num1 = this.parent.wbemObject.Put_(propertyName, 0, ref wmiValue, num); if (num1 < 0) { if (((long)num1 & (long)-4096) != (long)-2147217408) { Marshal.ThrowExceptionForHR(num1); } else { ManagementException.ThrowWithExtendedInfo((ManagementStatus)num1); return; } } return; } else { throw new InvalidOperationException(); } } else { throw new ArgumentNullException("propertyValue"); } }
/// <overload> /// <para>Adds a new <see cref='System.Management.PropertyData'/> with the specified value.</para> /// </overload> /// <summary> /// <para>Adds a new <see cref='System.Management.PropertyData'/> with the specified value. The value cannot /// be null and must be convertible to a CIM type.</para> /// </summary> /// <param name='propertyName'>The name of the new property.</param> /// <param name='propertyValue'>The value of the property (cannot be null).</param> /// <remarks> /// <para> Properties can only be added to class definitions, not /// to instances. This method is only valid when invoked on a <see cref='System.Management.PropertyDataCollection'/> /// in /// a <see cref='System.Management.ManagementClass'/>.</para> /// </remarks> public virtual void Add(string propertyName, object propertyValue) { if (null == propertyValue) { throw new ArgumentNullException(nameof(propertyValue)); } if (parent.GetType() == typeof(ManagementObject)) //can't add properties to instance { throw new InvalidOperationException(); } CimType cimType = 0; bool isArray = false; object wmiValue = PropertyData.MapValueToWmiValue(propertyValue, out isArray, out cimType); int wmiCimType = (int)cimType; if (isArray) { wmiCimType |= (int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY; } int status = parent.wbemObject.Put_(propertyName, 0, ref wmiValue, wmiCimType); if (status < 0) { if ((status & 0xfffff000) == 0x80041000) { ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); } else { Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f()); } } }