Exemplo n.º 1
0
        /// <summary>
        /// <para>Removes a <see cref='System.Management.PropertyData'/> from the <see cref='System.Management.PropertyDataCollection'/>.</para>
        /// </summary>
        /// <param name='propertyName'>The name of the property to be removed.</param>
        /// <remarks>
        ///    <para> Properties can only be removed from class definitions,
        ///       not from instances. This method is only valid when invoked on a property
        ///       collection in a <see cref='System.Management.ManagementClass'/>.</para>
        /// </remarks>
        /// <example>
        ///    <code lang='C#'>ManagementClass c = new ManagementClass("MyClass");
        /// c.Properties.Remove("PropThatIDontWantOnThisClass");
        ///    </code>
        ///    <code lang='VB'>Dim c As New ManagementClass("MyClass")
        /// c.Properties.Remove("PropThatIDontWantOnThisClass")
        ///    </code>
        /// </example>
        public virtual void Remove(string propertyName)
        {
            // On instances, reset the property to the default value for the class.
            if (parent.GetType() == typeof(ManagementObject))
            {
                ManagementClass cls = new ManagementClass(parent.ClassPath);
                parent.SetPropertyValue(propertyName, cls.GetPropertyValue(propertyName));
            }
            else
            {
                int status = parent.wbemObject.Delete_(propertyName);

                if (status < 0)
                {
                    if ((status & 0xfffff000) == 0x80041000)
                    {
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                    }
                    else
                    {
                        Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///    <para>Removes a property from the properties collection.</para>
        /// </summary>
        /// <param name='propertyName'>Specifies the name of the property to be removed.</param>
        /// <remarks>
        ///    <para>Properties can only be removed from class
        ///       definitions, not from instances, thus this method is only valid when invoked on
        ///       a Properties collection in a ManagementClass.</para>
        /// </remarks>
        /// <example>
        ///    <p>ManagementClass c = new ManagementClass("MyClass");</p>
        ///    <p>c.Properties.Remove("PropThatIDontWantOnThisClass");</p>
        /// </example>
        public virtual void Remove(string propertyName)
        {
            if (parent.GetType() == typeof(ManagementObject))             //can't remove properties to instance
            {
                throw new InvalidOperationException();
            }

            int status = parent.wbemObject.Delete_(propertyName);

            if (status < 0)
            {
                if ((status & 0xfffff000) == 0x80041000)
                {
                    ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                }
                else
                {
                    Marshal.ThrowExceptionForHR(status);
                }
            }
        }