I was setting up my new laptop this morning and I then started to get into configuring port forwarding on my router. Because this computer is becoming another ad-hoc development machine i decided to add it to the list of PC's i have that are able to be used remotely. To do this i needed to change the default port from 3389 to a custom port number. I again went through the hassle of looking on-line for a site that had the registry path to the terminal services RDP port number setting. It was at this point that either my lazy bone or my uber programming sense kicked in and i made a quick utility to use whenever i need to set the port number again. Thus saving me the hassle of again looking up the registry path, and then skimming through the registry hive looking for the setting using RegEdit.
Here is the code to a quick Console application program i wrote that can be used through command line argument or via standard command line interface.
1: using System;
2: using Microsoft.Win32;
3:
4: namespace RemotePortChanger
5: {
6: class Program
7: {
8: static void Main(string[] args)
9: {
10: int portNumber = 3389;
11:
12: try
13: {
14: if (args.Length > 0)
15: {
16: int.TryParse(args[0], out portNumber);
17:
18: SetPort(portNumber);
19: }
20: else
21: {
22: string input = string.Empty;
23:
24: Console.WriteLine("Remote Desktop Port: " + GetPort());
25: Console.Write("Enter the new port number: ");
26:
27: input = Console.ReadLine();
28:
29: int.TryParse(input, out portNumber);
30:
31: SetPort(portNumber);
32:
33: Console.WriteLine("Port is now " + GetPort() + " press any key to exit.");
34: Console.Read();
35: }
36: }
37: catch (Exception ex)
38: {
39: Console.WriteLine("Error has occured - " + ex.Message);
40: }
41: }
42:
43: private static void SetPort(object portNumber)
44: {
45: RegistryKey key;
46:
47: key = Registry.LocalMachine;
48: key = key.OpenSubKey(@"System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp", true);
49: key.SetValue("PortNumber", portNumber);
50: key.Close();
51: }
52:
53: private static int GetPort()
54: {
55: RegistryKey key;
56: int portNumber = 0;
57:
58: key = Registry.LocalMachine;
59: key = key.OpenSubKey(@"System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp");
60: portNumber = Convert.ToInt32(key.GetValue("PortNumber"));
61:
62: return portNumber;
63: }
64: }
65: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Programming, .Net