Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wmi

wmi-query

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?

like image 390
soapergem Avatar asked Jan 22 '15 00:01

soapergem


1 Answers

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;
like image 53
rducom Avatar answered Sep 23 '22 12:09

rducom