系统环境:

window10
visual studio 2019
.net framework 4.0
Microsoft RDP Client Control (redistributable) - version 7
 

步骤:

1、vs2019 新建一windows from桌面应用项目RemoteDesktopDemo,.net framework 4.0(低于此版本会出程序集不能加载错误)

2、添加Microsoft RDP Client Control (redistributable) - version 7控件

工具箱空白处鼠标右键->选择项

在com组件处选择Microsoft RDP Client Control  - version 7

确定后,工具箱上控件就可以使用了。

版本要求:

3、在Form1窗体上使用此控件,并添加服务器、用户名、密码等控件如图

单击Connect代码

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                rdp.Server = txtServer.Text;
                rdp.UserName = txtUserName.Text;

                IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
                secured.ClearTextPassword = txtPassword.Text;
                //special port
                //rdp.AdvancedSettings2.RDPPort = 3389;
                //share local drivers
                rdp.AdvancedSettings2.RedirectDrives = chbShareLocalDrivers.Checked;
               
               

                rdp.Connect();
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Error Connecting", "Error connecting to remote desktop " + txtServer.Text + " Error:  " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

单击disConnect代码

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                // Check if connected before disconnecting
                if (rdp.Connected.ToString() == "1")
                    rdp.Disconnect();
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Error Disconnecting", "Error disconnecting from remote desktop " + txtServer.Text + " Error:  " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

选中ShareLocalDrivers时,共享本地磁盘

4、运行结果

文章开头有本示例源码下载 ,或此处RemoteDesktopDemo例子下载
 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐