首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在C#中获取驱动程序的日期和版本?

在C#中获取驱动程序的日期和版本,可以使用Windows API函数。以下是一个示例代码:

代码语言:csharp
复制
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函数:

  • SetupDiGetClassDevs:获取设备信息集
  • SetupDiEnumDeviceInfo:枚举设备信息集中的设备
  • SetupDiGetDeviceRegistryProperty:获取设备注册表属性
  • SetupDiDestroyDeviceInfoList:销毁设备信息集

这些API函数可以帮助我们获取驱动程序的日期和版本。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

1分55秒

uos下升级hhdesk

3分59秒

基于深度强化学习的机器人在多行人环境中的避障实验

6分10秒

谈谈 Angular 的升级问题

5分5秒

什么是人工智能领域模型的 temperature 参数?

6分55秒

OpenSAP Fiori Elements 公开课第四单元

2分29秒

基于实时模型强化学习的无人机自主导航

14分35秒

Windows系统未激活或key不合适,导致内存只能用到2G

4分10秒

英语不好,对 SAP 英文文档有所畏惧,该怎么办?

领券