在Windows中获取驱动程序文件版本

时间:2015-04-14 09:01:18

标签: c# system drivers

如何通过c#在Windows中获取驱动程序文件的文件属性或版本信息。 我使用此代码:

var version = File.GetAttributes(Environment.SystemDirectory + @"\drivers\acpi.sys");

但此代码抛出异常:Could not find file 'C:\Windows\system32\drivers\acpi.sys'.。 然后我在dir变量中使用此代码var dir = Directory.GetFiles(Environment.SystemDirectory + @"\drivers");我有4个文件。如果我通过Windows资源管理器打开此文件夹,我有文件夹300+文件。我做错了什么?

2 个答案:

答案 0 :(得分:2)

您应该使用> = FrameWork .net 4, 添加:

using System.Management;

并添加引用 System.Management ;

        ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_PnPSignedDriver");
        ManagementObjectCollection moc = searcher.Get();

        foreach (var manObj in moc)
        {
            Console.WriteLine("Device Name:" + manObj["FriendlyName"] + " \r\nDeviceID: " + manObj["DeviceID"] + "\r\nDriverDate: " + manObj["DriverDate"] + "\r\nDriverVersion: " + manObj["DriverVersion"] + "\r\nDriverName:" + manObj["DriverName"] +"\n\r======================================\n\n";);  
        }

结果(部分):

enter image description here

答案 1 :(得分:1)

另一种方法是直接从文件读取版本。应该适用于64/32位应用程序,但是我不确定是否可以访问所有文件。

string directory;

if (Environment.Is64BitOperatingSystem)
    directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Sysnative");
else
    directory = Environment.GetFolderPath(Environment.SpecialFolder.System);

var info = FileVersionInfo.GetVersionInfo(Path.Combine(directory, "drivers", "acpi.sys"));

您可以在Folders under C:\Windows\System32\GroupPolicyUsers created with DirectoryFile System Redirector上找到有关Sysnative文件夹的更多信息