由于本人是做数据库维护经常使用到远程桌面,但是windows自带的远程桌面难以区分很不方便,所以我自己写了一个RDP。
RDP一共修改了两次。
第一种思路就是使用windows自带的RDP,保存成RDP文件,然后写个程序调用即可,但是这样有个弊端,在没有登录过的计算机上,需要重新输入用户名密码,十分苦恼。
try
{
System.Diagnostics.Process.Start("mstsc.exe", address + ".rdp");
}
catch (Exception)
{
MessageBox.Show("启动失败,可能没有远程文件");
}
于是摒弃了第一种思路,开始写第二种方法,自己可以手动添加RDP远程信息,然后调用COM组件来远程,这就涉及到数据存储,选择XML吧
这是在百度找的XML操作文档,个人感觉相当好用
public static class XmlHelper
{
public static string XmlSerialize(object o, Encoding encoding)
{
using (MemoryStream stream = new MemoryStream())
{
XmlSerializeInternal(stream, o, encoding);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream, encoding))
{
return reader.ReadToEnd();
}
}
}
///
/// 从XML字符串中反序列化对象
///
/// 结果对象类型
/// 包含对象的XML字符串 /// 编码方式 /// 反序列化得到的对象
public static List<RDP> XmlDeserialize(string s, Encoding encoding)
{
if (string.IsNullOrEmpty(s))
throw new ArgumentNullException("s");
if (encoding == null)
throw new ArgumentNullException("encoding");
XmlSerializer mySerializer = new XmlSerializer(typeof(List<RDP>));
using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s)))
{
using (StreamReader sr = new StreamReader(ms, encoding))
{
return (List<RDP>)mySerializer.Deserialize(sr);
}
}
}
private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding)
{
if (o == null)
throw new ArgumentNullException("o");
if (encoding == null)
throw new ArgumentNullException("encoding");
XmlSerializer serializer = new XmlSerializer(o.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.Encoding = encoding;
settings.IndentChars = " ";
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, o);
writer.Close();
}
}
///
/// 将一个对象按XML序列化的方式写入到一个文件
///
/// 要序列化的对象 /// 保存文件路径 /// 编码方式
public static void XmlSerializeToFile(object o, string path, Encoding encoding)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
{
XmlSerializeInternal(file, o, encoding);
}
}
///
/// 读入一个文件,并按XML的方式反序列化对象。
///
/// 结果对象类型
/// 文件路径 /// 编码方式 /// 反序列化得到的对象
public static List<RDP> XmlDeserializeFromFile(string path, Encoding encoding)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
if (encoding == null)
throw new ArgumentNullException("encoding");
string xml = File.ReadAllText(path, encoding);
return XmlDeserialize(xml, encoding);
}
}
我是将所有的RDP信息放在LIST里面,然后写在XML中,再读取出来。
下面就是RDP一些设置,我的思想上,用tabcontral,一个远程就动态构造一个tabpege但是没有关闭啊!于是我在百度上找了一个在tabpage上面绘画关闭按钮的代码,总之很多很多啊这是c#添加远程控件的方法
这里也很简单,就是一些设置前期怎么也找不到,后来发现并不是在构造的实例里面设置的。
IMsRdpClientAdvancedSettings7 AdvancedSettings7 = (IMsRdpClientAdvancedSettings7)RDP.AdvancedSettings;
AdvancedSettings7.RedirectClipboard = rdp.Clipbrd;
AdvancedSettings7.RedirectDrives = rdp.Disk;
if (rdp.Port != null && rdp.Port != "")
AdvancedSettings7.RDPPort = int.Parse(rdp.Port);
AdvancedSettings7.ClearTextPassword = rdp.PassWord;
AdvancedSettings7.ConnectToServerConsole = true;
AdvancedSettings7.ConnectToAdministerServer = true;
RDP.Connect();
tab_rdp.SelectedTab = tp;
原来还有个AdvancedSettings7 里面可以设置剪切板,磁盘映射之类的。