思路是首先新建一个vbs脚本,再创建一个bat脚本,再创建rdp文件,运行顺序是vbs->bat->rdp。rdp文件里面包含远程电脑的账密和其它信息,这样就可以不用再输入账密,而在程序里完成账密的设置,直接启动远程桌面(bat文件用vbs启动运行可以避免显示cmd的exe窗口),用C#语言写出这几个脚本,直接调用就可以实现功能(C#代码文尾),如下图

runBat.vbs文件里写

  1. set ws=WScript.CreateObject("WScript.Shell")
  2. ws.Run"runRdp.bat",

runRdp.bat文件里写

  1. mstsc rdesktop.rdp /console /v: 192.168.0.167:

rdesktop.rdp里面写如下代码,注意username和password分别是远程电脑的登录账密

  1. screen mode id:i:
  2. desktopwidth:i:
  3. desktopheight:i:
  4. session bpp:i:
  5. winposstr:s:,,,,,
  6. full address:s:MyServer
  7. compression:i:
  8. keyboardhook:i:
  9. audiomode:i:
  10. redirectdrives:i:
  11. redirectprinters:i:
  12. redirectcomports:i:
  13. redirectsmartcards:i:
  14. displayconnectionbar:i:
  15. autoreconnection
  16. enabled:i:
  17. username:s:Administrator
  18. domain:s:QCH
  19. alternate shell:s:
  20. shell working directory:s:
  21. password :b:01000000D08C9DDF0115D1118C7A00C04FC297EB01000000FBCFD336AC9B0D44B66EA56EE800C1E404000000020000000000106600000001000020000000EFDB7BB10E9F6509EEBF8C97E6BDC42BCB1E85AF5A801FC9623A21A4B628657B000000000E8000000002000020000000C0D7FAC8785CE745D7655BA1D97F2A16251EC23920D7B81DFE27BD29ED7A6D3910000000CAD751C2CEC0C749109F6C83AA90778F40000000751DB383D24B379C386A54EA93C50BA1B3AF96403D05BF252E7B10497C8BAE309AEF2F077C96EB241727A7D4023F7959DABFF48BC17615448681DAB3D1A3447A
  22. disable wallpaper:i:
  23. disable full window drag:i:
  24. disable menu anims:i:
  25. disable themes:i:
  26. disable cursor setting:i:
  27. bitmapcachepersistenable:i:

我们可以看到rdp文件里的密码很长,如下

  1. 01000000D08C9DDF0115D1118C7A00C04FC297EB01000000FBCFD336AC9B0D44B66EA56EE800C1E404000000020000000000106600000001000020000000EFDB7BB10E9F6509EEBF8C97E6BDC42BCB1E85AF5A801FC9623A21A4B628657B000000000E8000000002000020000000C0D7FAC8785CE745D7655BA1D97F2A16251EC23920D7B81DFE27BD29ED7A6D3910000000CAD751C2CEC0C749109F6C83AA90778F40000000751DB383D24B379C386A54EA93C50BA1B3AF96403D05BF252E7B10497C8BAE309AEF2F077C96EB241727A7D4023F7959DABFF48BC17615448681DAB3D1A3447A

密码是怎么来的呢,如下是加密算法,传入的参数pw是明文密码,比如上面的密码是123456,通过如下方法加密即可得到rdp里的密码

  1. /// <summary>
  2. /// 获取RDP密码
  3. /// </summary>
  4. private string GetRdpPassWord(string pw)
  5. {
  6. byte[] secret = Encoding.Unicode.GetBytes(pw);
  7. byte[] encryptedSecret = Protect(secret);
  8. string res = string.Empty;
  9. foreach (byte b in encryptedSecret)
  10. {
  11. res += b.ToString("X2"); //转换16进制的一定要用2位
  12.             }
  13. return res;
  14. }
  15.  
  16. /// <summary>
  17. /// 加密方法
  18. /// </summary>
  19. /// <param name="data"></param>
  20. /// <returns></returns>
  21. private static byte[] Protect(byte[] data)
  22. {
  23. try
  24. {
  25. //调用System.Security.dll
  26.                 return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
  27. }
  28. catch (CryptographicException e)
  29. {
  30. Console.WriteLine("Data was not encrypted. An error occurred.");
  31. Console.WriteLine(e.ToString());
  32. return null;
  33. }
  34. }

基本思路就是这样,附上打开远程桌面的C#类

  1. public class OpenRemoteDesktop
  2. {
  3. /// <summary>
  4. /// 密码因子
  5. /// </summary>
  6. static byte[] s_aditionalEntropy = null;
  7.  
  8. /// <summary>
  9. /// 打开远程桌面
  10. /// </summary>
  11. /// <param name="ip">IP地址</param>
  12. /// <param name="admin">用户名</param>
  13. /// <param name="pw">明文密码</param>
  14. public void Openrdesktop(string ip, string admin = null, string pw = null)
  15. {
  16. string password = GetRdpPassWord(pw);
  17.  
  18. #region newrdp
  19. //创建rdp
  20. string fcRdp = @"screen mode id:i:1
  21. desktopwidth:i:1280
  22. desktopheight:i:750
  23. session bpp:i:24
  24. winposstr:s:2,3,188,8,1062,721
  25. full address:s:MyServer
  26. compression:i:1
  27. keyboardhook:i:2
  28. audiomode:i:0
  29. redirectdrives:i:0
  30. redirectprinters:i:0
  31. redirectcomports:i:0
  32. redirectsmartcards:i:0
  33. displayconnectionbar:i:1
  34. autoreconnection
  35. enabled:i:1
  36. username:s:"+admin+@"
  37. domain:s:QCH
  38. alternate shell:s:
  39. shell working directory:s:
  40. password 51:b:"+password+@"
  41. disable wallpaper:i:1
  42. disable full window drag:i:1
  43. disable menu anims:i:1
  44. disable themes:i:0
  45. disable cursor setting:i:0
  46. bitmapcachepersistenable:i:1";
  47.  
  48. string rdpname = "rdesktop.rdp";
  49. CreationBat(rdpname, fcRdp);
  50. #endregion
  51.  
  52. //创建bat
  53. string fcBat = @"mstsc rdesktop.rdp /console /v:" + ip + ":3389";
  54. string batname = "runRdp.bat";
  55. CreationBat(batname, fcBat);
  56. //创建vbs
  57.  
  58. string vbsname = "runBat.vbs";
  59. string fcVbs = @"set ws=WScript.CreateObject(""WScript.Shell"")" + "\r\nws.Run\"runRdp.bat\",0";
  60. CreationBat(vbsname, fcVbs);
  61. //NewVbs(vbsname);
  62. ExecuteVbs(vbsname);
  63. }
  64.  
  65. /// <summary>
  66. /// 获取RDP密码
  67. /// </summary>
  68. private string GetRdpPassWord(string pw)
  69. {
  70. byte[] secret = Encoding.Unicode.GetBytes(pw);
  71. byte[] encryptedSecret = Protect(secret);
  72. string res = string.Empty;
  73. foreach (byte b in encryptedSecret)
  74. {
  75. res += b.ToString("X2"); //转换16进制的一定要用2位
  76.             }
  77. return res;
  78. }
  79.  
  80. /// <summary>
  81. /// 加密方法
  82. /// </summary>
  83. /// <param name="data"></param>
  84. /// <returns></returns>
  85. private static byte[] Protect(byte[] data)
  86. {
  87. try
  88. {
  89. //调用System.Security.dll
  90.                 return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
  91. }
  92. catch (CryptographicException e)
  93. {
  94. Console.WriteLine("Data was not encrypted. An error occurred.");
  95. Console.WriteLine(e.ToString());
  96. return null;
  97. }
  98. }
  99.  
  100. //解密方法
  101. private static byte[] Unprotect(byte[] data)
  102. {
  103. try
  104. {
  105.                 //Decrypt the data using DataProtectionScope.CurrentUser.
  106.                 return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
  107. }
  108. catch (CryptographicException e)
  109. {
  110. Console.WriteLine("Data was not decrypted. An error occurred.");
  111. Console.WriteLine(e.ToString());
  112. return null;
  113. }
  114. }
  115.  
  116. /// <summary>
  117. /// 创建bat脚本
  118. /// </summary>
  119. /// <param name="batName">文件名</param>
  120. /// <param name="fileContent">文件内容</param>
  121. /// <param name="u"></param>
  122. private void CreationBat(string batName, string fileContent, string u = null)
  123. {
  124. string filepath = System.IO.Directory.GetCurrentDirectory();
  125. string batpath = filepath + @"\" + batName;
  126. writeBATFile(fileContent, batpath);
  127. }
  128.  
  129. /// <summary>
  130. /// 写入文件
  131. /// </summary>
  132. /// <param name="fileContent">文件内容</param>
  133. /// <param name="filePath">路径</param>
  134. private void writeBATFile(string fileContent, string filePath)
  135. {
  136. if (!File.Exists(filePath))
  137. {
  138. FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件
  139. StreamWriter sw = new StreamWriter(fs1);
  140. sw.WriteLine(fileContent);//开始写入值
  141. sw.Close();
  142. fs1.Close();
  143. }
  144. else
  145. {
  146. FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
  147. StreamWriter sr = new StreamWriter(fs);
  148. sr.WriteLine(fileContent);//开始写入值
  149. sr.Close();
  150. fs.Close();
  151. }
  152. }
  153.  
  154. /// <summary>
  155. /// 调用执行bat文件
  156. /// </summary>
  157. /// <param name="batName">文件名</param>
  158. /// <param name="thisbatpath">路径</param>
  159. private void ExecuteBat(string batName, string thisbatpath)
  160. {
  161. Process proc = null;
  162. try
  163. {
  164. string targetDir = string.Format(thisbatpath);//this is where testChange.bat lies
  165. proc = new Process();
  166. proc.StartInfo.WorkingDirectory = targetDir;
  167. proc.StartInfo.FileName = batName;
  168. proc.StartInfo.Arguments = string.Format("");//this is argument
  169. proc.StartInfo.RedirectStandardError = false;
  170. proc.StartInfo.CreateNoWindow = true;
  171. proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
  172. proc.Start();
  173. proc.WaitForExit();
  174. }
  175. catch (Exception ex)
  176. {
  177. Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
  178. }
  179. }
  180.  
  181. /// <summary>
  182. /// 调用执行vbs文件
  183. /// </summary>
  184. /// <param name="vbsName">文件名</param>
  185. private void ExecuteVbs(string vbsName)
  186. {
  187. string path = System.IO.Directory.GetCurrentDirectory() + @"\" + vbsName;
  188. ProcessStartInfo startInfo = new ProcessStartInfo();
  189. startInfo.FileName = "wscript.exe";
  190. startInfo.Arguments = path;
  191. Process.Start(startInfo);
  192. }
  193.  
  194. /// <summary>
  195. /// 创建vbs文件
  196. /// </summary>
  197. /// <param name="vbsName">vbs文件名</param>
  198. private void NewVbs(string vbsName)
  199. {
  200. string path = System.IO.Directory.GetCurrentDirectory() + @"\" + vbsName;
  201. FileStream fsvbs = new FileStream(path, FileMode.Create, FileAccess.Write);
  202. StreamWriter runBat = new StreamWriter(path);
  203. runBat.WriteLine("set ws=WScript.CreateObject(\"WScript.Shell\")");
  204. runBat.WriteLine("ws.Run\"runRdp.bat\",0");
  205. runBat.Close();
  206. fsvbs.Close();
  207. }
  208. }

OpenRemoteDesktop

调用示例

  1. OpenRemoteDesktop m_open = new ServicesBLL.Common.OpenRemoteDesktop();
  2. m_open.Openrdesktop(ip, "Administrator","");

C#通过rdp账密直接打开远程桌面的更多相关文章

  1. Windows:打开MSDTC,恢复Windows任务栏,查看windows日志,打开远程桌面,打开Services,资源监控

    Windows 服务器系列: Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程 Windows:使用Dos命令管理服务(Services) Windows:任务调 ...

  2. windows7如何打开远程桌面&nbsp;-…

    单位的机器,刚装上了windows7旗舰版(当然不是花银子滴),想打开远程桌面连接,这样从别的机器登录也方便.可是问题来了,windows7对安全的设置比较高,不像windows XP那么随便一点就可 ...

  3. Windows 10打开远程桌面的方法

    今天使用windows 10,想要用远程桌面连接,可是怎么都找不到,哎,win10相比于win7和XP系统,感觉还是有点使用不习惯.不过后来还是找到了两个方法,在此记录下来,分享给需要的朋友. 1. ...

  4. 打开远程桌面时总提示无法打开连接文件default.rdp

    删除C:\Users\Administrator\Documents\default.rdp,再启动远程就好了 http://www.chahushequ.com/read-topic-94-2fa9 ...

  5. Ubuntu 12.04设置打开远程桌面登录1

    teamviewer_linux.deb sudo dpkg --install teamviewer_linux.deb

  6. 使用 OpenSSL为WindowsServer远程桌面(RDP)创建自签名证书 (Self-signed SSL certificate)

    前言 笔者查阅很多资料,才写成此文章,如有错误,请读者们及时提出. 一般大家使用远程桌面(Remote Desktop)连接Windows Server时,总会有一个警告提示,如图1 图1 出现此警告 ...

  7. 远程桌面协议RDP

    远程桌面协议RDP(Remove Desktop Protocol) 通过mstsc客户端远程连接计算机,并对其进行管理等操作. 与TELNET的区别在于,TELNET显示的是远程计算机的命令行窗口, ...

  8. 关于KeePass实现mstsc远程桌面(rdp协议)的自动登录

    本文的Keepass版本:KeePass Password Safe Version 2.45 首先介绍一下Keepass,引用官网的解释如下: KeePass is a free open sour ...

  9. CMD打开远程并使用空白密码远程登录

    记录一下,在单位管理局域网机器时 写出的小程序: 应用场景:比如异地A的局域网内主机需要远程登录进入系统调试,而A电脑的Radmin之类的远程控制软件无效,就只能使用操作系统自带的远程桌面功能,而,异 ...

随机推荐

  1. Ubuntu --- not enough free disk space

    Ubuntu系统更新时出现not enough free disk space. 原因是系统的就内核占满了/boot 的空间,只要将旧内核删除就ok了 首先,命令 uname -r  查看当前内核,( ...

  2. Message: u'The given selector btn dropdown-toggle btn-info is either invalid or does not result in a WebElement

    html代码: <html> <head> <meta http-equiv="content-type" content="text/ht ...

  3. 《Google软件测试之道》摘录

    以下是最近看的一本书<Google软件测试之道>里的一些摘录,收获很多. 1.讨论测试开发比并没有什么意义,如果你是一名开发人员,同时也是一名测试人员,如果你的职位头衔上有测试的字样,你的 ...

  4. Spring的3.0提供了一种:SpEL注入方式(了解)

    1. SpEL:Spring Expression Language是Spring的表达式语言,有一些自己的语法 2. 语法 * #{SpEL} 3. 例如如下的代码 <!-- SpEL的方式 ...

  5. PL/Sql快速执行 insert语句的.sql文件

    当全是 insert语句的.sql文件太大时(insert 语句条数太大),直接打开执行sql文件,pl/sql会卡死. 这是可以用pl/sql的命令窗口来执行.sql文件,操作步骤如下: 1.新建命 ...

  6. 13.8.8 div块 居中

    <div style="border:1px solid blue;width:760px; height:410px; position:absolute; left:50%; to ...

  7. IE浏览器调用jquery需要注意的小问题

    今天在进行前端重构的时候发现了一个非常奇怪的浏览器兼容性问题,我想在网页上放一个JS的特效,于是下载了jquery-easyui,经过修改完成所需要的效果后,准备放入项目中,发现在IE浏览器中无法运行 ...

  8. document.body和document.documentElement区别

    1.document.documentElement表示文档节点树的根节点,即<html> document.body是body节点 2. 页面具有 DTD,或者说指定了 DOCTYPE ...

  9. tty linux 打开和设置范例

    http://bbs.csdn.net/topics/340184140 /************************************************************** ...

  10. yersinia的DHCP池耗尽断网攻击

    http://jingyan.baidu.com/article/0eb457e5045bd703f1a9051d.html yersinia -G