Warm tip: This article is reproduced from serverfault.com, please click

OpenSubKey returns null in VB.NET

发布于 2012-07-11 06:57:45

I have a console program that read a registry key under HKCU:

OpenSubKey("Software\\Autodesk\\AutoCAD")

The program works very fine, but when I tried to use it as a Custom Action in an install project, it returned a null value.

Here are some suggestions:

  1. Turning off the UAC (user account control) will solve the problem.
  2. If I can run this console by double clicking it, it will work fine even if UAC is on.
Questioner
Mostafa abdo
Viewed
0
Damien_The_Unbeliever 2012-07-11 15:22:36

If it's returning null rather than throwing an exception, then it's not a permissions issue - it's that the key doesn't exist. OpenSubKey:

If the specified subkey cannot be found, then null is returned.

(And, note the exceptions listed above there):

SecurityException The user does not have the permissions required to read the registry key.

So, the likely issue is that your action is being invoked during the install sequence, when it's being run by the Windows Installer service - this service runs under the local system account, not under the logged in user's account.


Not knowing what you're trying to achieve with this key, I'm not sure what workaround to suggest. It may be that you can achieve your aims using standard built in facilities within the MSI system - or that you may want to defer this action until the first time you're program actually runs.

(What's possible inside the MSI may vary depending on what you're using to build it - if you're currently using a VS Setup project, you'll be quite limited)