How to enable Remote Desktop Connection Programmatically?

rreeves picture rreeves · Apr 30, 2012 · Viewed 9.2k times · Source

I'm trying to create a small application to setup fresh windows 7 systems. This is essentially so I can make images of the hard drives with all of the settings intact.

How would I go about enabling remote desktop from C# ?

I find it funny that everyone is flamming me but no one anwsered the question, sysprep cant do all of the required actions that I need in setting up the image. I want to enable RDP not run it. I will just change the registry key and add a firewall setting.

I will need this image to perform on several pieces of hardware.

Here is the laundry list of tasks I need to complete.

Static IP address, depends on computer. Change Folder Permissions, depends on Domain. Change Computer Name Install Rysnc Server Install Custom Applications Install Custom Services Firewall Permissions Drivers Disable Interactive Logon Change Date Time Depending on Location for System to be Sent Activate Windows Group Policy Settings.

I dont think that sysprep can do all these things can it?

Answer

itsme86 picture itsme86 · Apr 30, 2012

I've used the following in a previous project and it seems to work well:

        try
        {
            RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, TargetMachine.Name);
            key = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server", true);
            object val = key.GetValue("fDenyTSConnections");
            bool state = (int)val != 0;
            if (state)
            {
                key.SetValue("fDenyTSConnections", 0, RegistryValueKind.DWord);
                MessageBox.Show("Remote Desktop is now ENABLED");
            }
            else
            {
                key.SetValue("fDenyTSConnections", 1, RegistryValueKind.DWord);
                MessageBox.Show("Remote Desktop is now DISABLED");
            }
            key.Flush();
            if (key != null)
                key.Close();
        }
        catch
        {
            MessageBox.Show("Error toggling Remote Desktop permissions");
        }