probably at the beach
probably at the beach

Reputation: 15217

Accessing CLSID in the registry running as a service.

I'm running a C# service running under the System account trying to access the following registry key:

\CLSID{8E7FE1EC-79FA-43DF-9452-B71542599B3C}\InProcServer32\

The Key exists and I can access it during unit tests fine (unit tests run under the current user). The service can Access other keys in the Classes root but when I try to access anything in the CLSID subkey it fails.

           var regPermission = new RegistryPermission(RegistryPermissionAccess.AllAccess,
                                                       @"HKEY_CLASSES_ROOT\" + regPath);
            regPermission.Demand();
            using (var regKey = rootKey.OpenSubKey(regPath))
            {
                if (regKey != null)
                {
                    string defaultValue = (string)regKey.GetValue("");
                    {
                        return defaultValue;
                    }
                }
                else
                {
                    Logger.Info("Unable to open " + regPath);        
                }
            }

For anything beneath CLSID my code is returning 'Unable to open' + regPath..

Any tips as to why would be greatly appreciated

Upvotes: 0

Views: 751

Answers (1)

Chris Gessler
Chris Gessler

Reputation: 23123

This is happening because HKEY_CLASSES_ROOT is simply a pointer to HKEY_CURRENT_USER\Software\Classes and when noboy is logged in, well... then it won't exist. The real hive can be found under HKEY_USERS\{user_guid | .DEFAULT}\Software\Classes, so unless you know the user's GUID, you'll have to use .DEFAULT.

You might try using HKEY_LOCAL_MACHINE\Software\Classes instead.

Upvotes: 1

Related Questions