soapergem
soapergem

Reputation: 9989

How can I determine the version of a device driver when the device is not plugged in?

Thanks to some other helpful StackOverflow questions, I've found a way to query WMI for device drivers. However, it seems to me that the data is being stored in disparate places that don't join together well.

I have a USB-to-serial port cable using FTDI drivers. I can query Win32_SystemDrivers to determine whether or not the drivers have been installed, like this:

SelectQuery query = new SelectQuery("Win32_SystemDriver");
query.Condition = "Name = 'FTDIBUS'";

ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection drivers = searcher.Get();
bool installed = (drivers.Count > 0);

But that collection doesn't tell me a thing about version information. So then I discovered I could query Win32_PnPSignedDriver to find the version of the device driver. So I'm doing something like this:

SelectQuery query = new SelectQuery("Win32_PnPSignedDriver");
query.Condition = "DriverProviderName = 'FTDI'";

ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection drivers = searcher.Get();

foreach (ManagementBaseObject driverObject in drivers)
{
    ManagementObject driver = (ManagementObject)driverObject;
    string version = driver["DriverVersion"].ToString();
}

However, that second block of code will only succeed if the cable (device) is actually plugged in. What I'd like to do is to check the version of the installed device driver, regardless of whether or not the device is currently plugged in.

How do I do this?

Upvotes: 8

Views: 4913

Answers (2)

Hans Passant
Hans Passant

Reputation: 941465

Keep in mind that you are asking to solve the chicken-and-egg problem, never a very healthy start for any program. Plug & Play in Windows is built on the notion that device drivers can be dynamically located when the device becomes available, downloading it if necessary. That is a chicken.

You can only hope to find the egg if the driver was pre-installed or previously used. Not entirely uncommon. In that case, it will be available in the driver store. You should be able to find it back when you enumerate it.

A sample program that does this is available here. Written in C# so a good start for what you want. I'll copy the screenshot:

enter image description here

Note the displayed driver date and version, what you are asking about. Keep in mind that there can be more than a single version present. You might want to take a peek at the source, it is very simple. It actually relies on a Windows utility to populate the ListView, PnPUtil.exe with the /e option enumerates the store. Try that by running it from the command line.

You can also easily see the store content with Explorer, the files are stored in c:\windows\system32\driverstore\filerepository. Perhaps useful to read the .inf file to add additional filtering. The .inf filename you get back, listed in the 1st column in the screenshot, is actually the one that's copied into the c:\windows\inf directory.

Upvotes: 3

rducom
rducom

Reputation: 7320

It seems Win32_SystemDriver class can give you the PathName of the associated driver .sys file.

Example: "\SystemRoot\System32\drivers\afd.sys"

Then you can get the .sys file version with :

String path = @"C:\Windows\System32\drivers\afd.sys"
var myFileVersionInfo = FileVersionInfo.GetVersionInfo(path);
var version = myFileVersionInfo.ProductVersion;

Upvotes: 4

Related Questions