question

MarcGeorge-6477 avatar image
0 Votes"
MarcGeorge-6477 asked MarcGeorge-6477 answered

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

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?

dotnet-csharpwindows-api
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered MarcGeorge-6477 commented

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.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Copying your line of code and inserting it just before my existing code call generated the same error.

My code is slight different,

 internal string registryValue;
 internal string keyPath = @"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp";
 internal string keyValue = "PortNumber";

 RegistryKey rk = Registry.LocalMachine;
 rk.OpenSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);

 registryValue = (string)rk.GetValue(keyValue);




0 Votes 0 ·

rk.OpenSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);

Maybe path variable is incorrect.

1 Vote 1 ·

@Viorel-1 "Path" should have been "keyPath", but that didn't resolve the issue.

0 Votes 0 ·
DrakeWu-MSFT avatar image
0 Votes"
DrakeWu-MSFT answered DrakeWu-MSFT commented

Hi, @MarcGeorge-6477 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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DrakeWu-MSFT I copied your example directly and replaced mine and received the error again.

0 Votes 0 ·

Hi, @MarcGeorge-6477 I cannot reproduce your exception, even if I directly use @"RDP-Tcp", it only returns null, with c# console app in vs2019.
The code seems to be fine, the issue may be related to the environment,
What is the .net version you are using? Check if you meet the "Applies To" in the focument.
And what's the type of app you are developing, win32, uwp or any other app?
Or you could post a minimal, reproducible complete sample that can be compiled(without private information) so that we can help you handle this issue.
Another way is to use win32 api RegOpenKeyEx to open the specified registry key value, or directly use RegGetValue to get the value of lpValue under the key value of HKEY_LOCAL_MACHINE+lpSubKey.


0 Votes 0 ·
MarcGeorge-6477 avatar image
0 Votes"
MarcGeorge-6477 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.