How to enable Remote Desktop Connection Programmatically?

10,070

Solution 1

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");
        }

Solution 2

It is better to use the tool that comes with windows Sysprep. What it will do is prepare the system so you can do all the setup you want, run sysprep, then shutdown the computer and make the image. (here is a video tutorial on how to use Sysprep and ImageX the two windows tools designed to do EXACTLY what you are trying to do)

When you boot the image the first time it will go through the "Setup Windows" screens to enter things like the computer's name (or you can put in a xml file to skip that step and pre-fill out that information).

One big reason to do this is (and I got bit myself and that's how I learned about the tool) if you are using a domain each machines RID will be the same which will screw with your AD system.


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.

All of those things can be put in the unattend.xml Answer File and be set up. Here is a non video tutorial showing you how to create a unattend.xml file.

Solution 3

This code set 3 different value in registry: (I find registry changes with SysTracer v2.6)

            AllowRemoteAssistance = true;
            RemoteDesktopSelectNumber = 2;
            RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
            key = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Remote Assistance", true);
            if (AllowRemoteAssistance)
                key.SetValue("fAllowToGetHelp", 1, RegistryValueKind.DWord);
            else
                key.SetValue("fAllowToGetHelp", 0, RegistryValueKind.DWord);
            key.Flush();
            if (key != null)
                key.Close();

            if (RemoteDesktopSelectNumber == 1 || RemoteDesktopSelectNumber == 2)
            {
                key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
                key = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp\", true);
                key.SetValue("UserAuthentication", 0, RegistryValueKind.DWord);
                key.Flush();
                if (key != null)
                    key.Close();

                key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
                key = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server", true);
                if (RemoteDesktopSelectNumber == 1)
                    key.SetValue("fDenyTSConnections", 1, RegistryValueKind.DWord);
                else
                    key.SetValue("fDenyTSConnections", 0, RegistryValueKind.DWord);
                key.Flush();
                if (key != null)
                    key.Close();
            }
            else if (RemoteDesktopSelectNumber == 3)
            {
                key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
                key = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp\", true);
                key.SetValue("UserAuthentication", 1, RegistryValueKind.DWord);
                key.Flush();
                if (key != null)
                    key.Close();
            }
Share:
10,070
rreeves
Author by

rreeves

merge keep

Updated on June 27, 2022

Comments

  • rreeves
    rreeves almost 2 years

    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?

    • Daniel A. White
      Daniel A. White almost 12 years
      have you heard of sysprep? it makes stuff like this easier.
    • MUG4N
      MUG4N almost 12 years
    • Daniel A. White
      Daniel A. White almost 12 years
      @MUG4N - thats incorrect
    • Scott Chamberlain
      Scott Chamberlain almost 12 years
      "I dont think that sysprep can do all these things can it ?" To quote the description on the video I posted in my answer "This demonstration shows how to capture a custom Windows system image using the System Preparation Tool (sysprep) to generalize the installed image and ImageX to capture the contents of the generalized system image for re-deployment to other computers - including different hardware types." Emphasis mine.
  • Scott Chamberlain
    Scott Chamberlain almost 12 years
    This really is a "Old Shoe or Bottle?" question. It is better to give the correct way to do it instead of answering the question exactly as asked.
  • itsme86
    itsme86 almost 12 years
    I agree, but based on how the title is phrased, I thought I'd help people that stumble across this thread in the future too. After further explanation by the OP, I totally agree that the approach isn't the best.
  • Scott Chamberlain
    Scott Chamberlain almost 12 years
    @Bat Masterson you are going to run in to other issues than just "Turning on RDP" I really recommend using the correct way for cloning machines by using Sysprep
  • rreeves
    rreeves almost 12 years
    I'll watch the video and see whats up, thanks for the info. I upped
  • Jack
    Jack almost 8 years
    Wouldn't be better use a enum in RemoteDesktopSelectNumber?
  • Saher Ahwal
    Saher Ahwal over 7 years
    video link broken.