Remote Desktop Connection, RDP technical documentation

Source: Internet
Author: User

Turn theC # enables remote machine managementTools for remote control of remote hosts, process management, service management, and WMI-related information display, among others, there are still some problems have not been effectively resolved, I hope that have been involved or relevant experience of the predecessors can guide.

First, very rubbing the main interface

1, the configuration menu includes remote host connection configuration information to add and edit, the interface is as follows:

2, the Function menu includes remote control of remote host, remote management, service management and WMI information query;

3, after the completion of the addition, the function menu will change accordingly, such as:

Second, the main function realization part

Remote Host configuration information

    public class RemoteMachine {//<summary>///Desktop name///</summary> public s Tring Desktopname {get {return this._desktopname??? Server;        } set {this._desktopname = value;}}        private string _desktopname = null;        <summary>///The IP address or domain name of the Remote Desktop////</summary> public string Server {get; set;}        <summary>///user name///</summary> public string UserName {get; set;}        <summary>///IP//</summary> public string Domain {get; set;}        <summary>///Login password///</summary> public string Password {get; set;} <summary>///Allow query process///</summary> public bool Showprocess {get {return _showproce ss        } set {_showprocess = value;}}        private bool _showprocess = true; <summary>//Allow remote//</Summary> public bool Remoteable {get {return _remoteable;} set {_remoteable = value;}}        private bool _remoteable = true; <summary>///Allow query service///</summary> public bool Showservice {get {return _showservi Ce        } set {_showservice = value;}}        private bool _showservice = true; <summary>///shared disk drive///</summary> public bool Redirectdrives {get {return _redir ectdrives;        } set {_redirectdrives = value;}}        private bool _redirectdrives = true; <summary>///Shared printer///</summary> public bool Redirectprinters {get {return _redir Ectprinters;        } set {_redirectprinters = value;}}        private bool _redirectprinters = false; <summary>///Shared port///</summary> public bool Redirectports {get {return _REDIRECTP Orts;        } set {_redirectports = value;}} private bool _redirectPorts = false; <summary>///color depth///</summary> public Colors colordepth {get {return _colordepth ;        } set {_colordepth = value;}}        Private Colors _colordepth = Colors.color24; public override string ToString () {return string. Format ("{0}:{1}", this. Desktopname, this.        Server);        } public void Save (IniFile IniFile) {Save (this, IniFile); public void Delete (IniFile IniFile) {string-section = string. Format ("Remote ({0})", this.            Desktopname);        Inifile.deletesection (section); public void Load (IniFile IniFile, String desktopname) {string-section = string.            Format ("Remote ({0})", Desktopname); This.            Desktopname = Desktopname; This.            Server = inifile.readstring (section, "Server", ""); This.            UserName = inifile.readstring (section, "UserName", ""); This. Domain = Inifile.readstring (section, "Domain", ""); This.            Password = inifile.readstring (section, "Password", ""); This.            remoteable = inifile.readint (section, "Remoteable", 0) = = 1; This.            showprocess = inifile.readint (section, "Showprocess", 0) = = 1; This.            Showservice = inifile.readint (section, "Showservice", 0) = = 1; This.            redirectdrives = inifile.readint (section, "Redirectdrives", 0) = = 1; This.            Redirectprinters = inifile.readint (section, "Redirectprinters", 0) = = 1; This.            Redirectports = inifile.readint (section, "Redirectports", 0) = = 1; This.        colordepth = (Colors) inifile.readint (section, "ColorDepth", 0); } public static RemoteMachine Load (String desktopname, IniFile IniFile) {string-section = string .            Format ("Remote ({0})", Desktopname);            RemoteMachine mac = new RemoteMachine (); Mac.            Desktopname = Desktopname; Mac.  Server = inifile.readstring (section, "Server", "");          Mac.            UserName = inifile.readstring (section, "UserName", ""); Mac.            Domain = inifile.readstring (section, "Domain", ""); Mac.            Password = inifile.readstring (section, "Password", ""); Mac.            remoteable = inifile.readint (section, "Remoteable", 0) = = 1; Mac.            showprocess = inifile.readint (section, "Showprocess", 0) = = 1; Mac.            Showservice = inifile.readint (section, "Showservice", 0) = = 1; Mac.            redirectdrives = inifile.readint (section, "Redirectdrives", 0) = = 1; Mac.            Redirectprinters = inifile.readint (section, "Redirectprinters", 0) = = 1; Mac.            Redirectports = inifile.readint (section, "Redirectports", 0) = = 1; Mac.            colordepth = (Colors) inifile.readint (section, "ColorDepth", 0);        return mac; public static void-Save (RemoteMachine machine, IniFile IniFile) {string-section = string. Format ("Remote ({0})", Machine.            Desktopname); Inifile.writestring (sectiOn, "Desktopname", machine.            Desktopname); Inifile.writestring (section, "Server"), machine.            Server); Inifile.writestring (section, "UserName"), machine.            UserName); Inifile.writestring (section, ' Domain ', machine.            Domain); Inifile.writestring (section, "Password"), machine.            Password); Inifile.writeint (section, "Remoteable"), machine. Remoteable?            1:0); Inifile.writeint (section, "Showprocess"), machine. Showprocess?            1:0); Inifile.writeint (section, "Showservice"), machine. Showservice?            1:0); Inifile.writeint (section, "Redirectdrives"), machine. Redirectdrives?            1:0); Inifile.writeint (section, "Redirectprinters"), machine. Redirectprinters?            1:0); Inifile.writeint (section, "Redirectports"), machine. Redirectports?            1:0); Inifile.writeint (section, "ColorDepth", (int.) machine.        colordepth);          } public static list<remotemachine> Load (IniFile IniFile) {  string[] Infos = Inifile.readallsectionnames ();                if (infos! = null) {list<remotemachine> Macs = new list<remotemachine> (); foreach (string info in infos) {string-section = info. Substring (7, info.                    LENGTH-8);                    RemoteMachine mac = remotemachine.load (section, iniFile); Macs.                Add (MAC);            } return Macs;        } return null;    }} public enum Colors:int {Color8 = 8, Color16 = +, Color24 = 32, Color32 = }

1. Remote Control function

The remote control functions implemented within the tool need to refer to the AxInterop.MSTSCLib.dll and Interop.MSTSCLib.dll files, as follows:

    Public partial class Desktopform:form, Iremote {private AxMsRdpClient4 rdpc = null;        Public RemoteMachine machine {get; set;}            Public Desktopform (RemoteMachine machine) {InitializeComponent (); Text = string. Format ("" remote "{0}", machine.            Desktopname); This.        machine = machine; } private void Desktopform_load (object sender, EventArgs e) {this.rdpc = new AxMsRdpClient4 () {            Dock = DockStyle.Fill};            this.rdpc.OnDisconnected + = rdpc_ondisconnected; This.            Controls.Add (THIS.RDPC); This. Setrdpclientproperties (this.            Machine);        Connect (); } private void Rdpc_ondisconnected (object sender, Imstscaxevents_ondisconnectedevent e) {} PU blic bool Connect () {try {rdpc.                Connect ();            return true; } catch (Exception ex) {MessagebOx. Show (ex.                Message, "Warning", MessageBoxButtons.OK, messageboxicon.warning);            return false; }} public void Disconnect () {try {if (rdpc. Connected = = 1) rdpc.            Disconnect (); } catch (Exception ex) {MessageBox.Show (ex.            Message, "Warning", MessageBoxButtons.OK, messageboxicon.warning); }} private void Setrdpclientproperties (RemoteMachine machine) {rdpc. Server = machine.            Server; RDPC. UserName = machine.            UserName; RDPC. Domain = machine.            Domain; if (!string. IsNullOrEmpty (Machine. Password)) rdpc. Advancedsettings5.cleartextpassword = machine.            Password; RDPC. Advancedsettings5.redirectdrives = machine.            redirectdrives; RDPC. Advancedsettings5.redirectprinters = machine.            Redirectprinters; RDPC. Advancedsettings5.redirectports = machine. RedirecTports; RDPC. colordepth = (int) machine.            colordepth; RDPC.        Dock = DockStyle.Fill; }    }

By using the RemoteMachine parameter instantiation and remote control;

                    RemoteMachine mac = Item. Tag as RemoteMachine;                    using (desktopform df = new Desktopform (MAC))                    {                        df. ShowDialog ();                    }

2, process, service Management (WMI mode, need to inductive System.Management.dll class library, take process management as an example, ProcessInfo class contains property information can query MSDN, other similar)

Public partial class Processform:form, Iremote
{
Private Managementscope scope = null;
Private ManagementClass managementclass = null;
private string path = null;
Public RemoteMachine machine {get; set;}
Public ProcessForm (RemoteMachine machine)
{
InitializeComponent ();
Text = string. Format ("" process "{0}", machine. Desktopname);
This. machine = machine;
This.path = "\\\\" + this. Machine.server + "\\root\\cimv2:win32_process";
This.managementclass = new ManagementClass (This.path);
ConnectionOptions options = null;
if (this. Machine.server! = "." && this. Machine.username! = null && this. Machine.UserName.Length > 0)
{
options = new ConnectionOptions ();
Options. Username = this. Machine.username;
Options. Password = this. Machine.password;
Options. Enableprivileges = true;
Options. impersonation = ImpersonationLevel.Impersonate;
Options. authority = "Ntlmdomain:domain";
}
This.scope = new Managementscope ("\\\\" + this. Machine.server + "\\root\\cimv2", options);
This.managementClass.Scope = This.scope;
}

Private DataGridView DataGrid;
Private ContextMenuStrip ContextMenu;
Private processinfo[] processes;
private void Processform_load (object sender, EventArgs e)
{
This.contextmenu = new ContextMenuStrip ();
ToolStripItem item = THIS.CONTEXTMENU.ITEMS.ADD ("Stop");
Item. Click + = Item_click;
This.datagrid = new DataGridView () {Dock = DockStyle.Fill, ContextMenuStrip = this.contextmenu, SelectionMode = Datagridv Iewselectionmode.fullrowselect, Allowusertoaddrows = False, Allowusertodeleterows = False, Columnheadersheightsizemode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize, ReadOnly = True, rowheaderswidth = 21};
This. Controls.Add (This.datagrid);
ReLoad ();
}

private void ReLoad ()
{
This.dataGrid.DataSource = null;
this.processes = null;
if (Connect ())
{
processes = GetProcess ();
DataTable table = Processinfo.convertto (processes);
Binddata (table);
}
}

private void Item_click (object sender, EventArgs e)
{
if (This.dataGrid.SelectedRows.Count = = 1)
{
Try
{
UInt32 processId = Convert.touint32 (This.datagrid.selectedrows[0]. Cells[0]. Value);
String processName = This.datagrid.selectedrows[0]. CELLS[1]. Value.tostr

Remote Desktop Connection, RDP technical documentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.