OpenSubKey() Registry key's “Absolute Path”?

Marc George 21 Reputation points
2020-12-14T12:27:26.733+00:00

Using Microsoft.Win32.RegistryKey C# functions which require a registry path, like OpenSubKey(), using a path like @"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" generates an error stating “Absolute path information is required.”

What is the syntax to create the absolute path required?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,611 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,908 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 117.2K Reputation points
    2020-12-14T12:45:22.067+00:00

    This code seems to work:

    var k = Registry.LocalMachine.OpenSubKey( @"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" );
    

    Or maybe it did not guess your code.


  2. Drake Wu - MSFT 991 Reputation points
    2020-12-15T02:27:27.857+00:00

    Hi, @Marc George I assume that keyPath and path are the same value. I cannot reproduce your issue, but there are some problems with your code.

    1. rk.GetValue, you should use the RegistryKey object returned by OpenSubKey.
    2. PortNumber is of type REG_DWORD in registry and the value you get is a Int32, maybe you mean GetValue(keyValue).ToString();

    The following sample works for me:

                RegistryKey rk = Registry.LocalMachine;  
                string registryValue;  
                string keyValue = "PortNumber";  
                string keyPath = @"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp";  
                RegistryKey subkey = rk.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);  
                registryValue = subkey.GetValue(keyValue).ToString();  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Marc George 21 Reputation points
    2020-12-20T01:50:41.333+00:00

    From my research, key permissions that restrict access to READ operations of "ALL APPLICATION PACKAGES" will generate this error message.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.