简体   繁体   English

无法查询注册表中已安装的msi文件

[英]Unable to query registry for installed msi files

After windows installs a msi file, it will rename that file, move it to C:\\Windows\\Installers and write the info on how the file can be found to the registry. Windows安装一个msi文件后,它将重命名该文件,将其移动到C:\\ Windows \\ Installers并将关于如何找到该文件的信息写入注册表。

I want to query the registry for that key in order to get the exact location of the installed file. 我想在注册表中查询该注册表项,以获取已安装文件的确切位置。 The values I am looking for can be found at: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\ 我正在寻找的值可以在以下位置找到: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\

Unfortunately I won't know the exact ID of the installed file, so I'll have to open all the items listed at this location and check the product names. 不幸的是,我不知道已安装文件的确切ID,因此我必须打开此位置列出的所有项目并检查产品名称。

However when I try to query this part of the registry programmatically, I am unable to see any of the items that should be listed at this place. 但是,当我尝试以编程方式查询注册表的这一部分时,我看不到应该在此位置列出的任何项目。

This is the code I am using right now (no exception handling etc included): 这是我现在正在使用的代码(不包括异常处理等):

Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Installer").OpenSubKey("UserData").OpenSubKey("S-1-5-18").OpenSubKey("Products")

How can I query this part of the registry? 如何查询注册表的这一部分? Any other ideas on how to get the location (and name) of the msi file I am looking for? 关于如何获取我要查找的msi文件的位置(和名称)的其他想法?

Spelunking the registry is not the way to go. 摸索注册表不是走的路。 Windows Installer provides a Win32 API to access this information. Windows Installer提供了Win32 API来访问此信息。 For C#/.NET there is a very nice interop library to encapsulate all of this for you. 对于C#/。NET,有一个非常好的互操作库可以为您封装所有这些。 It's called Microsoft.Deployment.WindowsInstaller and ships with Windows Installer XML (WiX) which can be found on CodePlex. 它称为Microsoft.Deployment.WindowsInstaller,并随Windows Installer XML(WiX)一起提供,可以在CodePlex上找到它。

In this library is a class called ProductInstallation with a static method named GetProducts that wraps the underlying MsiEnumProducts function. 该库中有一个名为ProductInstallation的类,带有一个名为GetProducts的静态方法,该方法包装了基础MsiEnumProducts函数。

public static IEnumerable<ProductInstallation> GetProducts(
    string productCode,
    string userSid,
    UserContexts context
)

productCode (String) ProductCode (GUID) of the product instances to be enumerated. productCode(字符串)要枚举的产品实例的ProductCode(GUID)。 Only instances of products within the scope of the context specified by the userSid and context parameters will be enumerated. 仅枚举在userSid和context参数指定的上下文范围内的产品实例。 This parameter may be set to null to enumerate all products in the specified context. 可以将此参数设置为null以枚举指定上下文中的所有产品。

userSid (String) Specifies a security identifier userSid(字符串)指定安全标识符

(SID) that restricts the context of enumeration. (SID),用于限制枚举的上下文。 A SID value other than s-1-1-0 is considered a user SID and restricts enumeration to the current user or any user in the system. 除s-1-1-0之外的SID值被视为用户SID,并将枚举限制为当前用户或系统中的任何用户。 The special SID string s-1-1-0 (Everyone) specifies enumeration across all users in the system. 特殊的SID字符串s-1-1-0(每个人)指定系统中所有用户的枚举。 This parameter can be set to null to restrict the enumeration scope to the current user. 可以将此参数设置为null以将枚举范围限制为当前用户。 When context is set to the machine context only, userSid must be null. 当上下文仅设置为计算机上下文时,userSid必须为null。 context 上下文

(UserContexts) (UserContexts)

Specifies the user context. 指定用户上下文。

First of all, you can just write: 首先,您可以编写:

Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products")

There's no need to open each key individually. 无需单独打开每个键。

Second, I believe you problem might be that you're running the code from a 32-bit assembly on a 64-bit machine, and are trying to access the data found in the 64-bit part of the SOFTWARE key. 其次,我相信您的问题可能是您正在64位计算机上的32位程序集上运行代码,并试图访问SOFTWARE密钥的64位部分中的数据。 Instead, you are being rerouted to the Wow6432Node , where the key UserData does not necessarily exist inside Installer . 相反,您将被重新路由到Wow6432Node ,在该Wow6432Node ,键UserData不一定在Installer存在。

See this answer for how to read from the 64-bit part in a 32-bit assembly. 有关如何从32位程序集中的64位部分读取内容的信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM