By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

Read the Device manager programatically

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You could use WMI for this.
See the namespace System.Management.Instrumentation
-----Original Message-----
I was wondering if there is any way for me to read the device manager in myC# program? I have a program that crashes when some third party hardware isnot installed or is set to the wrong port and I could solve that problem ifI could read a couple entries from the registry.
Thanks in advance,
~Logan
.

Nov 15 '05 #2

P: n/a
That looks like it would have what I need but WMI has such a large scope I
was wondering if you might be able to point me to a specific part of WMI?
Thanks for all of your help,
~Logan
"Sunil" <an*******@discussions.microsoft.com> wrote in message
news:0e****************************@phx.gbl...
You could use WMI for this.
See the namespace System.Management.Instrumentation
-----Original Message-----
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
.

Nov 15 '05 #3

P: n/a
What stops you to simply read the Registry?

Willy.

"Logan McKinley" <lo***@globalweb.net> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #4

P: n/a
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]" <wi*************@pandora.be>
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

What stops you to simply read the Registry?

Willy.

"Logan McKinley" <lo***@globalweb.net> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #5

P: n/a
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(datet ime);
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.

"Logan McKinley" <lo***@globalweb.net> wrote in message news:O5*************@TK2MSFTNGP11.phx.gbl...
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]" <wi*************@pandora.be>
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

What stops you to simply read the Registry?

Willy.

"Logan McKinley" <lo***@globalweb.net> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
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



Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.