Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Wmi / managed / System / Management / PropertySet.cs / 1305376 / PropertySet.cs
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using WbemClient_v1;
using System.ComponentModel;
namespace System.Management
{
//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
///
/// Represents the set of properties of a WMI object.
///
///
/// using System;
/// using System.Management;
///
/// // This sample demonstrates how to enumerate properties
/// // in a ManagementObject object.
/// class Sample_PropertyDataCollection
/// {
/// public static int Main(string[] args) {
/// ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid = \"c:\"");
/// PropertyDataCollection diskProperties = disk.Properties;
/// foreach (PropertyData diskProperty in diskProperties) {
/// Console.WriteLine("Property = " + diskProperty.Name);
/// }
/// return 0;
/// }
/// }
///
/// Imports System
/// Imports System.Management
///
/// ' This sample demonstrates how to enumerate properties
/// ' in a ManagementObject object.
/// Class Sample_PropertyDataCollection
/// Overloads Public Shared Function Main(args() As String) As Integer
/// Dim disk As New ManagementObject("win32_logicaldisk.deviceid=""c:""")
/// Dim diskProperties As PropertyDataCollection = disk.Properties
/// Dim diskProperty As PropertyData
/// For Each diskProperty In diskProperties
/// Console.WriteLine("Property = " & diskProperty.Name)
/// Next diskProperty
/// Return 0
/// End Function
/// End Class
///
///
//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
public class PropertyDataCollection : ICollection, IEnumerable
{
private ManagementBaseObject parent;
bool isSystem;
internal PropertyDataCollection(ManagementBaseObject parent, bool isSystem) : base()
{
this.parent = parent;
this.isSystem = isSystem;
}
//
//ICollection
//
///
/// Gets or sets the number of objects in the .
///
///
/// The number of objects in the collection.
///
public int Count
{
get {
string[] propertyNames = null; object qualVal = null;
int flag;
if (isSystem)
flag = (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_SYSTEM_ONLY;
else
flag = (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_NONSYSTEM_ONLY;
flag |= (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_ALWAYS;
int status = parent.wbemObject.GetNames_(null, flag, ref qualVal, out propertyNames);
if (status < 0)
{
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else
Marshal.ThrowExceptionForHR(status);
}
return propertyNames.Length;
}
}
///
/// Gets or sets a value indicating whether the object is synchronized.
///
///
/// if the object is synchronized;
/// otherwise, .
///
public bool IsSynchronized { get { return false; }
}
///
/// Gets or sets the object to be used for synchronization.
///
///
/// The object to be used for synchronization.
///
public object SyncRoot { get { return this; }
}
///
/// Copies the into an array.
///
///
/// Copies the into an array.
///
/// The array to which to copy the .
/// The index from which to start copying.
public void CopyTo(Array array, Int32 index)
{
if (null == array)
throw new ArgumentNullException("array");
if ((index < array.GetLowerBound(0)) || (index > array.GetUpperBound(0)))
throw new ArgumentOutOfRangeException("index");
// Get the names of the properties
string[] nameArray = null;
object dummy = null;
int flag = 0;
if (isSystem)
flag |= (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_SYSTEM_ONLY;
else
flag |= (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_NONSYSTEM_ONLY;
flag |= (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_ALWAYS;
int status = this.parent.wbemObject.GetNames_(null, flag, ref dummy, out nameArray);
if (status >= 0)
{
if ((index + nameArray.Length) > array.Length)
throw new ArgumentException(null,"index");
foreach (string propertyName in nameArray)
array.SetValue(new PropertyData(parent, propertyName), index++);
}
if (status < 0)
{
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else
Marshal.ThrowExceptionForHR(status);
}
return;
}
///
/// Copies the to a specialized object
/// array.
///
/// The destination array to contain the copied .
/// The index in the destination array from which to start copying.
public void CopyTo(PropertyData[] propertyArray, Int32 index)
{
CopyTo((Array)propertyArray, index);
}
//
// IEnumerable
//
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)(new PropertyDataEnumerator(parent, isSystem));
}
///
/// Returns the enumerator for this .
///
///
/// An
/// that can be used to iterate through the collection.
///
public PropertyDataEnumerator GetEnumerator()
{
return new PropertyDataEnumerator(parent, isSystem);
}
//Enumerator class
///
/// Represents the enumerator for
/// objects in the .
///
///
/// using System;
/// using System.Management;
///
/// // This sample demonstrates how to enumerate all properties in a
/// // ManagementObject using the PropertyDataEnumerator object.
/// class Sample_PropertyDataEnumerator
/// {
/// public static int Main(string[] args) {
/// ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID='C:'");
/// PropertyDataCollection.PropertyDataEnumerator propertyEnumerator = disk.Properties.GetEnumerator();
/// while(propertyEnumerator.MoveNext()) {
/// PropertyData p = (PropertyData)propertyEnumerator.Current;
/// Console.WriteLine("Property found: " + p.Name);
/// }
/// return 0;
/// }
/// }
///
/// Imports System
/// Imports System.Management
///
/// ' This sample demonstrates how to enumerate all properties in a
/// ' ManagementObject using PropertyDataEnumerator object.
/// Class Sample_PropertyDataEnumerator
/// Overloads Public Shared Function Main(args() As String) As Integer
/// Dim disk As New ManagementObject("Win32_LogicalDisk.DeviceID='C:'")
/// Dim propertyEnumerator As _
/// PropertyDataCollection.PropertyDataEnumerator = disk.Properties.GetEnumerator()
/// While propertyEnumerator.MoveNext()
/// Dim p As PropertyData = _
/// CType(propertyEnumerator.Current, PropertyData)
/// Console.WriteLine("Property found: " & p.Name)
/// End While
/// Return 0
/// End Function
/// End Class
///
///
public class PropertyDataEnumerator : IEnumerator
{
private ManagementBaseObject parent;
private string[] propertyNames;
private int index;
internal PropertyDataEnumerator(ManagementBaseObject parent, bool isSystem)
{
this.parent = parent;
propertyNames = null; index = -1;
int flag; object qualVal = null;
if (isSystem)
flag = (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_SYSTEM_ONLY;
else
flag = (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_NONSYSTEM_ONLY;
flag |= (int)tag_WBEM_CONDITION_FLAG_TYPE.WBEM_FLAG_ALWAYS;
int status = parent.wbemObject.GetNames_(null, flag, ref qualVal, out propertyNames);
if (status < 0)
{
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else
Marshal.ThrowExceptionForHR(status);
}
}
///
object IEnumerator.Current { get { return (object)this.Current; } }
///
/// Gets the current in the enumeration.
///
///
/// The current
/// element in the collection.
///
public PropertyData Current
{
get {
if ((index == -1) || (index == propertyNames.Length))
throw new InvalidOperationException();
else
return new PropertyData(parent, propertyNames[index]);
}
}
///
/// Moves to the next element in the
/// enumeration.
///
///
/// if the enumerator was successfully advanced to the next element;
/// if the enumerator has passed the end of the collection.
///
public bool MoveNext()
{
if (index == propertyNames.Length) //passed the end of the array
return false; //don't advance the index any more
index++;
return (index == propertyNames.Length) ? false : true;
}
///
/// Resets the enumerator to the beginning of the
/// enumeration.
///
public void Reset()
{
index = -1;
}
}//PropertyDataEnumerator
//
// Methods
//
///
/// Returns the specified property from the , using [] syntax.
///
/// The name of the property to retrieve.
///
/// A , based on
/// the name specified.
///
///
/// ManagementObject o = new ManagementObject("Win32_LogicalDisk.Name = 'C:'");
/// Console.WriteLine("Free space on C: drive is: ", c.Properties["FreeSpace"].Value);
///
/// Dim o As New ManagementObject("Win32_LogicalDisk.Name=""C:""")
/// Console.WriteLine("Free space on C: drive is: " & c.Properties("FreeSpace").Value)
///
///
public virtual PropertyData this[string propertyName]
{
get {
if (null == propertyName)
throw new ArgumentNullException("propertyName");
return new PropertyData(parent, propertyName);
}
}
///
/// Removes a from the .
///
/// The name of the property to be removed.
///
/// Properties can only be removed from class definitions,
/// not from instances. This method is only valid when invoked on a property
/// collection in a .
///
///
/// ManagementClass c = new ManagementClass("MyClass");
/// c.Properties.Remove("PropThatIDontWantOnThisClass");
///
/// Dim c As New ManagementClass("MyClass")
/// c.Properties.Remove("PropThatIDontWantOnThisClass")
///
///
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);
}
}
}
///
/// Adds a new with the specified value.
///
///
/// Adds a new with the specified value. The value cannot
/// be null and must be convertable to a CIM type.
///
/// The name of the new property.
/// The value of the property (cannot be null).
///
/// Properties can only be added to class definitions, not
/// to instances. This method is only valid when invoked on a
/// in
/// a .
///
public virtual void Add(string propertyName, Object propertyValue)
{
if (null == propertyValue)
throw new ArgumentNullException("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);
}
}
///
/// Adds a new with the specified value and CIM type.
///
/// The name of the property.
/// The value of the property (which can be null).
/// The CIM type of the property.
///
/// Properties can only be added to class definitions, not
/// to instances. This method is only valid when invoked on a
/// in
/// a .
///
public void Add(string propertyName, Object propertyValue, CimType propertyType)
{
if (null == propertyName)
throw new ArgumentNullException("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);
}
}
///
/// Adds a new with no assigned value.
///
/// The name of the property.
/// The CIM type of the property.
/// to specify that the property is an array type; otherwise, .
///
/// Properties can only be added to class definitions, not
/// to instances. This method is only valid when invoked on a
/// in
/// a .
///
public void Add(string propertyName, CimType propertyType, bool isArray)
{
if (null == propertyName)
throw new ArgumentNullException(propertyName);
if (parent.GetType() == typeof(ManagementObject)) //can't add properties to instance
throw new InvalidOperationException();
int wmiCimType = (int)propertyType;
if (isArray)
wmiCimType = (wmiCimType | (int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY);
object dummyObj = System.DBNull.Value;
int status = parent.wbemObject.Put_(propertyName, 0, ref dummyObj, wmiCimType);
if (status < 0)
{
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else
Marshal.ThrowExceptionForHR(status);
}
}
}//PropertyDataCollection
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- MessageDecoder.cs
- WebPartVerb.cs
- HttpClientChannel.cs
- ConstraintStruct.cs
- NotFiniteNumberException.cs
- HyperLinkField.cs
- Currency.cs
- DiagnosticsConfiguration.cs
- ChannelCredentials.cs
- DataGridViewCellStyle.cs
- WorkflowInlining.cs
- DataObjectAttribute.cs
- StrokeCollectionConverter.cs
- MergablePropertyAttribute.cs
- WpfKnownTypeInvoker.cs
- XmlExtensionFunction.cs
- BitmapEffectOutputConnector.cs
- BulletChrome.cs
- IncrementalCompileAnalyzer.cs
- NumberSubstitution.cs
- ListControlConvertEventArgs.cs
- Positioning.cs
- ClientUrlResolverWrapper.cs
- ButtonPopupAdapter.cs
- WinFormsComponentEditor.cs
- DictionaryEntry.cs
- DataGridRowDetailsEventArgs.cs
- AutomationAttributeInfo.cs
- AspNetHostingPermission.cs
- HashCoreRequest.cs
- XmlAttributes.cs
- ValidatingPropertiesEventArgs.cs
- WindowsFont.cs
- Function.cs
- XPathException.cs
- TableColumnCollectionInternal.cs
- HMACMD5.cs
- EntityCollectionChangedParams.cs
- Panel.cs
- DataMemberConverter.cs
- ApplicationTrust.cs
- Ref.cs
- GridItemCollection.cs
- DataGridColumnHeadersPresenter.cs
- ObjectSpanRewriter.cs
- DataGridViewElement.cs
- ISessionStateStore.cs
- ClientTarget.cs
- UiaCoreApi.cs
- SimpleBitVector32.cs
- ExtensionFile.cs
- UserPreferenceChangedEventArgs.cs
- WebReference.cs
- TextControl.cs
- RecordManager.cs
- PositiveTimeSpanValidatorAttribute.cs
- Constants.cs
- LayoutEngine.cs
- ColumnResizeAdorner.cs
- Point3D.cs
- SafeNativeMethods.cs
- TableColumn.cs
- RoutedEvent.cs
- HostedTransportConfigurationManager.cs
- MatrixCamera.cs
- PieceDirectory.cs
- SimpleApplicationHost.cs
- srgsitem.cs
- DataGridViewColumnCollection.cs
- httpserverutility.cs
- InternalPermissions.cs
- WindowsGrip.cs
- CodeDirectiveCollection.cs
- ValueTable.cs
- MimeWriter.cs
- EllipticalNodeOperations.cs
- Label.cs
- TagMapCollection.cs
- RepeatButtonAutomationPeer.cs
- CommandExpr.cs
- UTF8Encoding.cs
- ExtensionDataReader.cs
- RtfControlWordInfo.cs
- BuilderPropertyEntry.cs
- AssemblyFilter.cs
- RelatedCurrencyManager.cs
- ServicePointManager.cs
- ObjectDataSourceView.cs
- LocatorGroup.cs
- DrawItemEvent.cs
- PositiveTimeSpanValidator.cs
- SqlSelectStatement.cs
- TextRangeEdit.cs
- WindowsFormsSectionHandler.cs
- FileFormatException.cs
- IsolatedStorageFileStream.cs
- StringUtil.cs
- OracleCommandBuilder.cs
- ProtectedConfiguration.cs
- Message.cs