1

I want to read the port number for remote desktops, but it doesn't work.

MSDN states that it is at

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp\PortNumber

which is a key that exists in my registry on Windows 8.

private void Form1_Load(object sender, EventArgs e) { txtPort.Text = (string) Registry.GetValue(@"HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp", "PortNumber", ""); } 

The string returned is null.

Correct answer:

private void Form1_Load(object sender, EventArgs e) { txtPort.Text = Registry.GetValue(@"HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp\", "PortNumber", -1).ToString(); } 
4
  • 5
    Have you checked the actual registry to see whether there's a value there? Commented Aug 30, 2013 at 13:20
  • 1
    Are you running Visual Studio with a user that has rights to check the registry ? try runnin VS as Administrator Commented Aug 30, 2013 at 13:22
  • I did check the registry, but not for spaces. My mistake. Commented Aug 30, 2013 at 13:30
  • I did run as administrator just to be sure, because I read that writing to the registry requires this. Commented Aug 30, 2013 at 13:30

1 Answer 1

7

There's a space in "Terminal Server":

@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" ↑ 
Sign up to request clarification or add additional context in comments.

5 Comments

And it's an int, not a string.
@JonSkeet, He's just showing that on a textfield right ? So does it matter ?
@ShankarDamodaran: Yes, it matters. He's casting to string, which will fail when the value returned is a boxed int.
Thanks. My programming world falls apart when copy/paste doesn't work
@KasperHansen: The natural thing to do when diagnosing this is just to run regedit and validate it for yourself. (That's what I did, and was about to post a similar answer when I saw that dtb had already done so.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.