try
{
ConnectionOptions connection = new ConnectionOptions();
connection.Username = userNameBox.Text;
connection.Password = passwordBox.Text;
connection.Authority = "ntlmdomain:Finance123";
ManagementScope scope = new ManagementScope(
"\\dell417\root\CIMV2", connection);
scope.Connect();
ObjectQuery query= new ObjectQuery(
"SELECT * FROM Win32_DiskDrive");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("");
Console.WriteLine("Win32_DiskDrive instance");
Console.WriteLine("");
Console.WriteLine("Availability: {0}", queryObj["Availability"]);
Console.WriteLine("BytesPerSector: {0}", queryObj["BytesPerSector"]);
if(queryObj["Capabilities"] == null)
Console.WriteLine("Capabilities: {0}", queryObj["Capabilities"]);
else
{
UInt16[] arrCapabilities = (UInt16[])(queryObj["Capabilities"]);
foreach (UInt16 arrValue in arrCapabilities)
{
Console.WriteLine("Capabilities: {0}", arrValue);
}
}
if(queryObj["CapabilityDescriptions"] == null)
Console.WriteLine("CapabilityDescriptions: {0}", queryObj["CapabilityDescriptions"]);
else
{
String[] arrCapabilityDescriptions = (String[])(queryObj["CapabilityDescriptions"]);
foreach (String arrValue in arrCapabilityDescriptions)
{
Console.WriteLine("CapabilityDescriptions: {0}", arrValue);
}
}
Console.WriteLine("Caption: {0}", queryObj["Caption"]);
Console.WriteLine("CompressionMethod: {0}", queryObj["CompressionMethod"]);
Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);
Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);
Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);
Console.WriteLine("DefaultBlockSize: {0}", queryObj["DefaultBlockSize"]);
Console.WriteLine("Description: {0}", queryObj["Description"]);
Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]);
Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]);
Console.WriteLine("ErrorMethodology: {0}", queryObj["ErrorMethodology"]);
Console.WriteLine("Index: {0}", queryObj["Index"]);
Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
Console.WriteLine("InterfaceType: {0}", queryObj["InterfaceType"]);
Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]);
Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
Console.WriteLine("MaxBlockSize: {0}", queryObj["MaxBlockSize"]);
Console.WriteLine("MaxMediaSize: {0}", queryObj["MaxMediaSize"]);
Console.WriteLine("MediaLoaded: {0}", queryObj["MediaLoaded"]);
Console.WriteLine("MediaType: {0}", queryObj["MediaType"]);
Console.WriteLine("MinBlockSize: {0}", queryObj["MinBlockSize"]);
Console.WriteLine("Model: {0}", queryObj["Model"]);
Console.WriteLine("Name: {0}", queryObj["Name"]);
Console.WriteLine("NeedsCleaning: {0}", queryObj["NeedsCleaning"]);
Console.WriteLine("NumberOfMediaSupported: {0}", queryObj["NumberOfMediaSupported"]);
Console.WriteLine("Partitions: {0}", queryObj["Partitions"]);
Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
if(queryObj["PowerManagementCapabilities"] == null)
Console.WriteLine("PowerManagementCapabilities: {0}", queryObj["PowerManagementCapabilities"]);
else
{
UInt16[] arrPowerManagementCapabilities = (UInt16[])(queryObj["PowerManagementCapabilities"]);
foreach (UInt16 arrValue in arrPowerManagementCapabilities)
{
Console.WriteLine("PowerManagementCapabilities: {0}", arrValue);
}
}
Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);
Console.WriteLine("SCSIBus: {0}", queryObj["SCSIBus"]);
Console.WriteLine("SCSILogicalUnit: {0}", queryObj["SCSILogicalUnit"]);
Console.WriteLine("SCSIPort: {0}", queryObj["SCSIPort"]);
Console.WriteLine("SCSITargetId: {0}", queryObj["SCSITargetId"]);
Console.WriteLine("SectorsPerTrack: {0}", queryObj["SectorsPerTrack"]);
Console.WriteLine("Signature: {0}", queryObj["Signature"]);
Console.WriteLine("Size: {0}", queryObj["Size"]);
Console.WriteLine("Status: {0}", queryObj["Status"]);
Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);
Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]);
Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);
Console.WriteLine("TotalCylinders: {0}", queryObj["TotalCylinders"]);
Console.WriteLine("TotalHeads: {0}", queryObj["TotalHeads"]);
Console.WriteLine("TotalSectors: {0}", queryObj["TotalSectors"]);
Console.WriteLine("TotalTracks: {0}", queryObj["TotalTracks"]);
Console.WriteLine("TracksPerCylinder: {0}", queryObj["TracksPerCylinder"]);
}
Close();
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);
}
catch(System.UnauthorizedAccessException unauthorizedErr)
{
MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
}