2022.3.3 更新:附上前端代码
BTW:由于这些代码是刚出来工作时写的代码,所以代码质量可能比较差。也懒得去改了。现在主要用.net 6了。
-------------------------------分割线---------------------------------------
因公司业务需求需要,需要多次远程连接到各个服务器中进行操作,所以该软件主要实现多远程桌面的合并,可用选项卡切换。并可调整远程桌面大小,解决小屏远程之后需要频繁拖动滚动条的不便之处。同时会通过读取ini配置文件对远程桌面连接的相关数据进行保存。
详情请看代码,欢迎指导与交流。
后端代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Management;
using System.Threading;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security;
namespace maintainance
{
public partial class windows_terminal : Form
{
public windows_terminal()
{
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section,
string key,
string val,
string filePath);
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string lpAppName,//section名
string lpKeyName,//key名
string lpDefault,//当查找不到相应值时返回的值
StringBuilder lpReturnedString,//将查找到的值返回到指定缓冲区
int nSize,//返回的最大字符串长度
string lpFileName);//文件名
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string lpAppName,
string lpKeyName,
string lpDefault,
byte[] lpReturnedString,
int nSize,
string lpFileName);
private string path = "c:/config.ini";//登录信息保存文件
public string Path
{
get { return path; }
}
private void button1_Click(object sender, EventArgs e)//连接按钮
{
TabPage SelectPage = tabControl1.TabPages[tabControl1.SelectedIndex];
AxMSTSCLib.AxMsRdpClient7 rdp = (AxMSTSCLib.AxMsRdpClient7)(tabControl1.TabPages[tabControl1.SelectedIndex].Controls["rdp"]);
if (rdp.Connected != 0)//若该窗体已连接,则断开连接
{
DisconnectDelegate dd = new DisconnectDelegate(disconnect);
dd.Invoke(ref rdp);
}
try
{
if (SelectPage.Controls["panel"].Controls["ip"].Text == "")
{
MessageBox.Show("请输入IP地址");
return;
}
rdp.Server = SelectPage.Controls["panel"].Controls["ip"].Text;
rdp.UserName = SelectPage.Controls["panel"].Controls["username"].Text;
rdp.AdvancedSettings2.ClearTextPassword = SelectPage.Controls["panel"].Controls["password"].Text;
rdp.Connect();
SelectPage.Text = SelectPage.Controls["panel"].Controls["ip"].Text + "("+SelectPage.Controls["panel"].Controls["nickname"].Text+")";
CheckBox c = (CheckBox)SelectPage.Controls["panel"].Controls["isRemember"];
if(c.Checked)//如果记住密码选项已勾选,则将相关信息加密后写入相应目录
{
WritePrivateProfileString(rdp.Server, "username",encrypt(rdp.UserName), Path);
WritePrivateProfileString(rdp.Server, "password", encrypt(SelectPage.Controls["panel"].Controls["password"].Text), Path);
WritePrivateProfileString(rdp.Server,"nickname",encrypt(SelectPage.Controls["panel"].Controls["nickname"].Text),Path);
}
}
catch
{
MessageBox.Show("连接失败");
}
}
private void windows_terminal_FormClosing(object sender, FormClosingEventArgs e)//当窗体关闭时所有窗口断开连接
{