C#制作简易远程桌面工具

起因

  • 因为工作需要,几乎每天都要用到远程桌面连接,有时候会连接4-5台服务器,每次都mstsc来连接,时间长了就觉得很烦人,于是乎做个简易的管理工具来连接。

环境

  • .Net 4.0
  • 使用com组件Microsoft Terminal Services Client Control
  • IDE visual studio 2019

主要代码及注释

// 动态创建一个新窗体,用于满屏装载远程桌面
Form form = new Form();
// 隐藏窗口Icon图标
form.ShowIcon = false;
//form.StartPosition = FormStartPosition.Manual;
// 窗口名称,这里用传过来的IP去掉.命名
form.Name = ServerIps[0].Replace(".", "");
// 窗口文本,备注(IP地址)
form.Text = string.Format("{0} ({1})", args[3], ServerIps[0]);
// 窗口大小为 1024 * 768
form.Size = new Size(1024, 768);

// 获取屏幕尺寸
Rectangle ScreenArea = Screen.PrimaryScreen.Bounds;
// 创建RdpClient
AxMsRdpClient7NotSafeForScripting axMsRdpc = new AxMsRdpClient7NotSafeForScripting();
((System.ComponentModel.ISupportInitialize)(axMsRdpc)).BeginInit();
axMsRdpc.Dock = DockStyle.Fill;
axMsRdpc.Enabled = true;
axMsRdpc.Name = string.Format("axMsRdpc_{0}", ServerIps[0].Replace(".", ""));

// 绑定连接与释放事件
axMsRdpc.OnConnecting += new EventHandler(this.axMsRdpc_OnConnecting);
axMsRdpc.OnDisconnected += new IMsTscAxEvents_OnDisconnectedEventHandler(this.axMsRdpc_OnDisconnected);

// 将COM组件Rdpc添加到新窗口中
form.Controls.Add(axMsRdpc);
// 打开新窗口
form.Show();
((System.ComponentModel.ISupportInitialize)(axMsRdpc)).EndInit();

// 服务器地址
axMsRdpc.Server = ServerIps[0];
// 远程登录账号
axMsRdpc.UserName = args[1];
// 远程端口号
axMsRdpc.AdvancedSettings7.RDPPort = ServerIps.Length == 1 ? 3389 : Convert.ToInt32(ServerIps[1]);
// 自动控制屏幕显示尺寸
//axMsRdpc.AdvancedSettings9.SmartSizing = true;
// 启用CredSSP身份验证(有些服务器连接没有反应,需要开启这个)
axMsRdpc.AdvancedSettings7.EnableCredSspSupport = true;
// 远程登录密码
axMsRdpc.AdvancedSettings7.ClearTextPassword = args[2];
// 颜色位数 8,16,24,32
axMsRdpc.ColorDepth = 32;
// 开启全屏 true|flase
axMsRdpc.FullScreen = this.isFullScreen;
// 设置远程桌面宽度为显示器宽度
axMsRdpc.DesktopWidth = ScreenArea.Width;
// 设置远程桌面宽度为显示器高度
axMsRdpc.DesktopHeight = ScreenArea.Height;
// 远程连接
axMsRdpc.Connect();

效果预览

main

add