问题
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?
回答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");
}
回答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.
回答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();
}
来源:https://stackoverflow.com/questions/10387299/how-to-enable-remote-desktop-connection-programmatically