You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Windows系统中通过编程获取驱动程序安装日期

获取驱动程序安装日期的替代方案

好问题!确实Win32_PnPSignedDriverInstallDate属性经常返回null——这是因为很多驱动在安装过程中并没有正确填充这个字段。下面分享几个更可靠的方法:

方法1:WMI结合注册表查询

Win32_PnPEntity类能获取设备的基础信息,我们可以通过它的DeviceIDService名称,定位到注册表中存储的驱动安装日期:

  • 注册表路径HKLM\SYSTEM\CurrentControlSet\Services\<ServiceName>中,InstallDate是一个DWORD值(记录从1970年1月1日开始的秒数),转换后即可得到可读日期
  • 也可以访问HKLM\SYSTEM\CurrentControlSet\Enum\<DeviceID>\<实例ID>\Driver,这里的InstallDate是二进制FILETIME格式,同样可转换为日期

PowerShell示例

# 筛选目标设备(这里以显卡为例,可替换为你需要的设备名称)
$device = Get-WmiObject Win32_PnPEntity | Where-Object {$_.Name -like "*显卡*"}
$serviceName = $device.Service

# 查询注册表并转换安装日期
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName"
$installSec = (Get-ItemProperty -Path $regPath -Name InstallDate).InstallDate
$installDate = (Get-Date "1970-01-01").AddSeconds($installSec)

Write-Host "驱动安装日期:$installDate"

方法2:使用Windows SetupAPI(C++/C#)

Windows原生的SetupAPI提供了专门的设备安装属性查询接口,其中SPDRP_INSTALL_DATE字段能直接获取驱动安装日期,这是最可靠的方式之一。

C#简化示例

using System;
using System.Runtime.InteropServices;

public class DriverDateHelper
{
    [DllImport("setupapi.dll", SetLastError = true)]
    private static extern bool SetupDiGetDeviceRegistryProperty(
        IntPtr deviceInfoSet, 
        ref SP_DEVINFO_DATA deviceInfoData, 
        uint property, 
        out uint propertyRegDataType, 
        IntPtr propertyBuffer, 
        uint propertyBufferSize, 
        out uint requiredSize);

    [StructLayout(LayoutKind.Sequential)]
    private struct SP_DEVINFO_DATA
    {
        public uint cbSize;
        public Guid ClassGuid;
        public uint DevInst;
        public IntPtr Reserved;
    }

    private const uint SPDRP_INSTALL_DATE = 0x19;

    public static DateTime? GetInstallDate(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceData)
    {
        byte[] buffer = new byte[8];
        uint regType, requiredSize;
        
        if (SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceData, SPDRP_INSTALL_DATE, out regType, 
            Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0), (uint)buffer.Length, out requiredSize))
        {
            long fileTime = BitConverter.ToInt64(buffer, 0);
            return DateTime.FromFileTime(fileTime);
        }
        return null;
    }
}

方法3:驱动文件日期(替代方案)

如果以上方法都无法获取到准确值,你可以查看驱动核心文件(比如.sys文件)的修改日期或数字签署日期——虽然这不是严格意义上的安装日期,但多数情况下和安装时间高度接近。

PowerShell示例

# 获取驱动文件路径
$service = Get-WmiObject Win32_SystemDriver | Where-Object {$_.Name -eq $serviceName}
$driverPath = $service.PathName.Replace("`"", "")

# 获取文件修改日期
$fileModifyDate = (Get-Item $driverPath).LastWriteTime
Write-Host "驱动文件修改日期:$fileModifyDate"

内容的提问来源于stack exchange,提问作者gajapathy p

火山引擎 最新活动