Read the Device manager programatically

  • Thread starter Thread starter Logan McKinley
  • Start date Start date
L

Logan McKinley

I was wondering if there is any way for me to read the device manager in my
C# program? I have a program that crashes when some third party hardware is
not installed or is set to the wrong port and I could solve that problem if
I could read a couple entries from the registry.
Thanks in advance,
~Logan
 
I am not sure where in the registry that information is located. I need to
make sure an installed serial port (USB/Serial Port emulator) is available
and the device description. So an enumeration of serial ports with their
descriptions would be perfect.
Thanks,
~Logan
----- Original Message -----
From: "Willy Denoyette [MVP]" <[email protected]>
Newsgroups:
microsoft.public.dotnet.framework,microsoft.public.dotnet.languages.csharp
Sent: Wednesday, October 29, 2003 1:23 PM
Subject: Re: Read the Device manager programatically
 
Logan,

Herewith a small console program that enumerates all devices connected to the system and print their respective properties.
If you find your device(s) description in the output , it will be possible to refine the query for just the devices you are
interested in.


using System;
using System.Management;
// Enum Pnp Registered devices using WMI class Win32_PnPentity
class App {
public static void Main() {
ManagementPath path = new ManagementPath();
ManagementClass devs = null;
path.Server = ".";
path.NamespacePath = @"root\CIMV2";
path.RelativePath = @"Win32_PnPentity";
using(devs = new ManagementClass(new ManagementScope(path), path, new ObjectGetOptions(null, new TimeSpan(0,0,0,2), true)))
{
ManagementObjectCollection moc = devs.GetInstances();
foreach(ManagementObject mo in moc) {

PropertyDataCollection devsProperties = mo.Properties;
foreach (PropertyData devProperty in devsProperties ) {
if (devProperty.Type == CimType.DateTime) {
if(devProperty.Value != null)
Console.WriteLine("Date {0}", ToDateTime(devProperty.Value.ToString()));
}
else
Console.WriteLine("Property = {0}\t Value = {1}",
devProperty.Name, devProperty.Value);
}

RelatedObjectQuery relatedQuery = new RelatedObjectQuery
("associators of {Win32_PnPEntity.DeviceID='" + mo["DeviceID"]+ "'}");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(new ManagementScope(path),relatedQuery);
foreach (ManagementObject mob in searcher.Get()) {

Console.WriteLine("--------------------------->>>>>>");
Console.WriteLine(mob["Description"]);

}
Console.WriteLine("----------------------");
}

}

}
// Converts a given datetime in DMTF format to System.DateTime object.
static System.DateTime ToDateTime(string dmtfDate) {
int year = System.DateTime.MinValue.Year;
int month = System.DateTime.MinValue.Month;
int day = System.DateTime.MinValue.Day;
int hour = System.DateTime.MinValue.Hour;
int minute = System.DateTime.MinValue.Minute;
int second = System.DateTime.MinValue.Second;
long ticks = 0;
string dmtf = dmtfDate;
System.DateTime datetime = System.DateTime.MinValue;
string tempString = System.String.Empty;
if ((dmtf == null)) {
throw new System.ArgumentOutOfRangeException();
}
if ((dmtf.Length == 0)) {
throw new System.ArgumentOutOfRangeException();
}
if ((dmtf.Length != 25)) {
throw new System.ArgumentOutOfRangeException();
}
try {
tempString = dmtf.Substring(0, 4);
if (("****" != tempString)) {
year = System.Int32.Parse(tempString);
}
tempString = dmtf.Substring(4, 2);
if (("**" != tempString)) {
month = System.Int32.Parse(tempString);
}
tempString = dmtf.Substring(6, 2);
if (("**" != tempString)) {
day = System.Int32.Parse(tempString);
}
tempString = dmtf.Substring(8, 2);
if (("**" != tempString)) {
hour = System.Int32.Parse(tempString);
}
tempString = dmtf.Substring(10, 2);
if (("**" != tempString)) {
minute = System.Int32.Parse(tempString);
}
tempString = dmtf.Substring(12, 2);
if (("**" != tempString)) {
second = System.Int32.Parse(tempString);
}
tempString = dmtf.Substring(15, 6);
if (("******" != tempString)) {
ticks = (System.Int64.Parse(tempString)
* (System.TimeSpan.TicksPerMillisecond / 1000));
}
if (((((((((year < 0)
|| (month < 0))
|| (day < 0))
|| (hour < 0))
|| (minute < 0))
|| (minute < 0))
|| (second < 0))
|| (ticks < 0))) {
throw new System.ArgumentOutOfRangeException();
}
}
catch (System.Exception e) {
e = e;
throw new System.ArgumentOutOfRangeException();
}
datetime = new System.DateTime(year, month, day, hour, minute, second, 0);
datetime = datetime.AddTicks(ticks);
System.TimeSpan tickOffset = System.TimeZone.CurrentTimeZone.GetUtcOffset(datetime);
int UTCOffset = 0;
long OffsetToBeAdjusted = 0;
long OffsetMins = (tickOffset.Ticks / System.TimeSpan.TicksPerMinute);
tempString = dmtf.Substring(22, 3);
if ((tempString != "******")) {
tempString = dmtf.Substring(21, 4);
try {
UTCOffset = System.Int32.Parse(tempString);
}
catch (System.Exception e) {
e = e;
throw new System.ArgumentOutOfRangeException();
}
OffsetToBeAdjusted = (OffsetMins - UTCOffset);
datetime = datetime.AddMinutes(OffsetToBeAdjusted);
}
return datetime;
}
}

Willy.
 
Back
Top