在C#中获取驱动程序的日期和版本,可以使用Windows API函数。以下是一个示例代码:
using System;
using System.Runtime.InteropServices;
public class DriverInfo
{
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid,
[MarshalAs(UnmanagedType.LPTStr)] string Enumerator,
IntPtr hwndParent,
uint Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern bool SetupDiEnumDeviceInfo(
IntPtr DeviceInfoSet,
uint MemberIndex,
ref SP_DEVINFO_DATA DeviceInterfaceData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiGetDeviceRegistryProperty(
IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
uint Property,
out uint PropertyRegDataType,
IntPtr PropertyBuffer,
uint PropertyBufferSize,
out uint RequiredSize);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern bool SetupDiDestroyDeviceInfoList(
IntPtr DeviceInfoSet);
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVINFO_DATA
{
public uint cbSize;
public Guid ClassGuid;
public uint DevInst;
public IntPtr Reserved;
}
public static void Main()
{
Guid guid = new Guid();
IntPtr deviceInfoSet = SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, 4);
SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA();
deviceInfoData.cbSize = (uint)Marshal.SizeOf(deviceInfoData);
uint memberIndex = 0;
while (SetupDiEnumDeviceInfo(deviceInfoSet, memberIndex, ref deviceInfoData))
{
memberIndex += 1;
uint property = 2; // SPDRP_DRIVERVERSION
uint propertyRegDataType;
uint requiredSize;
SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, property, out propertyRegDataType, IntPtr.Zero, 0, out requiredSize);
IntPtr propertyBuffer = Marshal.AllocHGlobal((int)requiredSize);
SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, property, out propertyRegDataType, propertyBuffer, requiredSize, out requiredSize);
string driverVersion = Marshal.PtrToStringAuto(propertyBuffer);
Marshal.FreeHGlobal(propertyBuffer);
Console.WriteLine("Driver version: " + driverVersion);
}
SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
}
这个示例代码将枚举所有设备,并获取它们的驱动程序版本。请注意,这个示例代码可能需要管理员权限才能运行。
在这个示例中,我们使用了以下Windows API函数:
这些API函数可以帮助我们获取驱动程序的日期和版本。
领取专属 10元无门槛券
手把手带您无忧上云