远程桌面连接,rdp技术文档记录

转的

C#实现远程机器管理

工具,用于对远程主机进行远程控制、进程管理、服务管理以及WMI相关信息显示等;其中仍然存在部分问题还没有得到有效的解决,希望曾经参与过或者有关相关经验的前辈能够指导一下。

一、很搓很搓的主界面

1、配置菜单里面包括远程主机连接配置信息的添加和编辑,界面如下:

2、功能菜单里面包括对远程主机远程控制、远程管理、服务管理和WMI信息查询;

3、添加完成后,功能菜单会进行相应的变化,如下图:

二、主要功能实现部分

远程主机配置信息

    public class RemoteMachine
    {
        /// <summary>
        /// 桌面名称
        /// </summary>
        public string DesktopName { get { return this._DesktopName ?? this.Server; } set { this._DesktopName = value; } }
        private string _DesktopName = null;
        /// <summary>
        /// 远程桌面的IP地址或者域名
        /// </summary>
        public string Server { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// IP
        /// </summary>
        public string Domain { get; set; }
        /// <summary>
        /// 登录密码
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 允许查询进程
        /// </summary>
        public bool ShowProcess { get { return _ShowProcess; } set { _ShowProcess = value; } }
        private bool _ShowProcess = true;
        /// <summary>
        /// 允许远程
        /// </summary>
        public bool RemoteAble { get { return _RemoteAble; } set { _RemoteAble = value; } }
        private bool _RemoteAble = true;
        /// <summary>
        /// 允许查询服务
        /// </summary>
        public bool ShowService { get { return _ShowService; } set { _ShowService = value; } }
        private bool _ShowService = true;
        /// <summary>
        /// 共享磁盘驱动器
        /// </summary>
        public bool RedirectDrives { get { return _RedirectDrives; } set { _RedirectDrives = value; } }
        private bool _RedirectDrives = true;
        /// <summary>
        /// 共享打印机
        /// </summary>
        public bool RedirectPrinters { get { return _RedirectPrinters; } set { _RedirectPrinters = value; } }
        private bool _RedirectPrinters = false;
        /// <summary>
        /// 共享端口
        /// </summary>
        public bool RedirectPorts { get { return _RedirectPorts; } set { _RedirectPorts = value; } }
        private bool _RedirectPorts = false;
        /// <summary>
        /// 色彩深度
        /// </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 = 16,
        Color24 = 24,
        Color32 = 32
    }

1、远程控制功能

工具内实现的远程控制功能需要引用AxInterop.MSTSCLib.dll和Interop.MSTSCLib.dll文件,具体代码如下:

    public partial class DesktopForm : Form, IRemote
    {
        private AxMsRdpClient4 rdpc = null;
        public RemoteMachine Machine { get; set; }
        public DesktopForm(RemoteMachine machine)
        {
            InitializeComponent();
            Text = string.Format("【远程】{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)
        {

        }

        public bool Connect()
        {
            try
            {
                rdpc.Connect();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
        }

        public void Disconnect()
        {
            try
            {
                if (rdpc.Connected == 1)
                    rdpc.Disconnect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", 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;
        }
    }

通过使用RemoteMachine参数实例化并进行远程控制;

                    RemoteMachine mac = item.Tag as RemoteMachine;
                    using (DesktopForm df = new DesktopForm(mac))
                    {
                        df.ShowDialog();
                    }

2、进程、服务管理等(WMI方式,需要引人System.Management.dll类库,以进程管理为例,ProcessInfo类包含的属性信息可查询MSDN,其他类似)

    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("【进程】{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("停止");
item.Click += item_Click;
this.dataGrid = new DataGridView() { Dock = DockStyle.Fill, ContextMenuStrip = this.contextMenu, SelectionMode = DataGridViewSelectionMode.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

时间: 2024-08-02 07:03:36

远程桌面连接,rdp技术文档记录的相关文章

清除&ldquo;远程桌面连接&rdquo;的历史记录

清除"远程桌面连接"的历史记录 首先来明确一下我们接下来要清除的数据,通过我们的PC端连接其他客户端或服务器后会在自己PC端的"远程桌面连接"工具中留下访问记录,在打开"远程桌面连接"工具后单击下拉箭头即可显示(打开远程桌面连接可以在 开始>运行 中输入mstsc回车,或依次单击"开始>所有程序>附件>远程桌面连接"打开): 清除工作如下 Step1.  点击"开始->运行",

bat管理mstsc 远程桌面连接

批量添加用户 @echo off set "用户名文件=a.txt" set "用户组名称=administrators" ::文件路径可以有空格,但是不需要额外加"引号 for /f "usebackq tokens=1-3" %%a in ("%用户名文件%") do ( net user %%a %%b /add /PASSWORDCHG:%%c net LOCALGROUP %用户组名称% %%a  /add

删除远程桌面连接记录

1.修改注册表:开始-运行-regedit找到HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Default,删除右面的数值.这个办法只可以清除下拉框里的,但是最后一次你连接的还是显示在上面的.2.在"我的文档"下,把隐藏的"Default.rdp"删了.通过上面两条就可以完全删除远程桌面输入过的ip表单记录了. 删除远程桌面连接记录,布布扣,bubuko.com

远程桌面连接记录彻底清除

远程桌面连接是微软从Windows2000开始就自带的比较实用的组件之一,因其简单方便且免费,许多商务白领用它来远程办公,网管用它来远程维护服务 器,个人外出旅行时用它来连接家中的电脑,黑客用它来控制肉鸡等等,哈哈,说的太多了,入正题. 用过远程桌面的朋友都知道,远程桌面只要成功登录过一次,就会很人性化的把远程主机名,IP等记录下来,给用户下次连接带来很大方便:但有些时候,我们因 为隐私等原因,并不想保留这些记录,但问题是微软从来就没为远程桌面提供过删除记录之类的选项,连最新发布的Windows

技术文档链接记录

本博文不具备参考价值,仅作为本人自己的文档记录!!! 好一出骂架,吓得我都不敢随便说话了: 语言中,静态方法和非静态方法你懂多少? 头脑发昏记录了这个,我只需要像约翰·卡马克一样专注就好了:程序员的五点建议--如何成为编程高手并以此创业 原文地址:https://www.cnblogs.com/Moyar/p/11617140.html

远程桌面Default.rdp 中各个参数的含义

存储在 Default.rdp 文件中的设置 默认情况下,将在“我的文档”文件夹中创建 Default.rdp 文件.以下 RDP 设置存储在 Desktop.rdp 文件中: desktopwidth:i 此设置对应于您在远程桌面连接“选项”中的“显示”选项卡上选择的桌面宽度. 注意:基于 Microsoft Windows CE 的设备只支持全屏模式. desktopheight:i 此设置对应于您在远程桌面连接“选项”中的“显示”选项卡上选择的桌面高度. 注意:基于 Microsoft W

Boost.Asio技术文档

Christopher Kohlhoff Copyright ? 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_0.txt文件或从http://www.boost.org/LICENSE_1_0.txt) Boost.Asio是用于网络和低层IO编程的跨平台C++库,为开发者提供了C++环境下稳定的异步模型. 综述 基本原理 应用程序与外界交互的方式有很多,可通过文件,网络,串口或控制台.例如在网络通信中,完

远程桌面Default.rdp 中各个参数的含义(转)

存储在 Default.rdp 文件中的设置 默认情况下,将在“我的文档”文件夹中创建 Default.rdp 文件.以下 RDP 设置存储在 Desktop.rdp 文件中: desktopwidth:i 此设置对应于您在远程桌面连接“选项”中的“显示”选项卡上选择的桌面宽度. 注意:基于 Microsoft Windows CE 的设备只支持全屏模式. desktopheight:i 此设置对应于您在远程桌面连接“选项”中的“显示”选项卡上选择的桌面高度. 注意:基于 Microsoft W

通过xrdp实现远程桌面连接Windows Azure linux虚拟机

本文以Oracle Linux 6.4虚拟机为示例(22及3389端口必须打开,分别用于SSH及RDP连接) 1.在安装xrdp之前,首先需要安装一些必要的包,如: # yum -y install kernel-headers # yum -y install gcc openssl pam-devel openssl-devel # yum -y install autoconf automake libtool libX11-devel libXfixes-devel # yum -y i