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

runBat.vbs文件里写

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

runRdp.bat文件里写

mstsc rdesktop.rdp /console /v: 192.168.0.167:

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

screen mode id:i:
desktopwidth:i:
desktopheight:i:
session bpp:i:
winposstr:s:,,,,,
full address:s:MyServer
compression:i:
keyboardhook:i:
audiomode:i:
redirectdrives:i:
redirectprinters:i:
redirectcomports:i:
redirectsmartcards:i:
displayconnectionbar:i:
autoreconnection
enabled:i:
username:s:Administrator
domain:s:QCH
alternate shell:s:
shell working directory:s:
password :b:01000000D08C9DDF0115D1118C7A00C04FC297EB01000000FBCFD336AC9B0D44B66EA56EE800C1E404000000020000000000106600000001000020000000EFDB7BB10E9F6509EEBF8C97E6BDC42BCB1E85AF5A801FC9623A21A4B628657B000000000E8000000002000020000000C0D7FAC8785CE745D7655BA1D97F2A16251EC23920D7B81DFE27BD29ED7A6D3910000000CAD751C2CEC0C749109F6C83AA90778F40000000751DB383D24B379C386A54EA93C50BA1B3AF96403D05BF252E7B10497C8BAE309AEF2F077C96EB241727A7D4023F7959DABFF48BC17615448681DAB3D1A3447A
disable wallpaper:i:
disable full window drag:i:
disable menu anims:i:
disable themes:i:
disable cursor setting:i:
bitmapcachepersistenable:i:

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

01000000D08C9DDF0115D1118C7A00C04FC297EB01000000FBCFD336AC9B0D44B66EA56EE800C1E404000000020000000000106600000001000020000000EFDB7BB10E9F6509EEBF8C97E6BDC42BCB1E85AF5A801FC9623A21A4B628657B000000000E8000000002000020000000C0D7FAC8785CE745D7655BA1D97F2A16251EC23920D7B81DFE27BD29ED7A6D3910000000CAD751C2CEC0C749109F6C83AA90778F40000000751DB383D24B379C386A54EA93C50BA1B3AF96403D05BF252E7B10497C8BAE309AEF2F077C96EB241727A7D4023F7959DABFF48BC17615448681DAB3D1A3447A

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

 /// <summary>
/// 获取RDP密码
/// </summary>
private string GetRdpPassWord(string pw)
{
byte[] secret = Encoding.Unicode.GetBytes(pw);
byte[] encryptedSecret = Protect(secret);
string res = string.Empty;
foreach (byte b in encryptedSecret)
{
res += b.ToString("X2"); //转换16进制的一定要用2位
            }
return res;
} /// <summary>
/// 加密方法
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private static byte[] Protect(byte[] data)
{
try
{
//调用System.Security.dll
                return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not encrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
}

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

  public class OpenRemoteDesktop
{
/// <summary>
/// 密码因子
/// </summary>
static byte[] s_aditionalEntropy = null; /// <summary>
/// 打开远程桌面
/// </summary>
/// <param name="ip">IP地址</param>
/// <param name="admin">用户名</param>
/// <param name="pw">明文密码</param>
public void Openrdesktop(string ip, string admin = null, string pw = null)
{
string password = GetRdpPassWord(pw); #region newrdp
//创建rdp
string fcRdp = @"screen mode id:i:1
desktopwidth:i:1280
desktopheight:i:750
session bpp:i:24
winposstr:s:2,3,188,8,1062,721
full address:s:MyServer
compression:i:1
keyboardhook:i:2
audiomode:i:0
redirectdrives:i:0
redirectprinters:i:0
redirectcomports:i:0
redirectsmartcards:i:0
displayconnectionbar:i:1
autoreconnection
enabled:i:1
username:s:"+admin+@"
domain:s:QCH
alternate shell:s:
shell working directory:s:
password 51:b:"+password+@"
disable wallpaper:i:1
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
disable cursor setting:i:0
bitmapcachepersistenable:i:1"; string rdpname = "rdesktop.rdp";
CreationBat(rdpname, fcRdp);
#endregion //创建bat
string fcBat = @"mstsc rdesktop.rdp /console /v:" + ip + ":3389";
string batname = "runRdp.bat";
CreationBat(batname, fcBat);
//创建vbs string vbsname = "runBat.vbs";
string fcVbs = @"set ws=WScript.CreateObject(""WScript.Shell"")" + "\r\nws.Run\"runRdp.bat\",0";
CreationBat(vbsname, fcVbs);
//NewVbs(vbsname);
ExecuteVbs(vbsname);
} /// <summary>
/// 获取RDP密码
/// </summary>
private string GetRdpPassWord(string pw)
{
byte[] secret = Encoding.Unicode.GetBytes(pw);
byte[] encryptedSecret = Protect(secret);
string res = string.Empty;
foreach (byte b in encryptedSecret)
{
res += b.ToString("X2"); //转换16进制的一定要用2位
            }
return res;
} /// <summary>
/// 加密方法
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private static byte[] Protect(byte[] data)
{
try
{
//调用System.Security.dll
                return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not encrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
} //解密方法
private static byte[] Unprotect(byte[] data)
{
try
{
                //Decrypt the data using DataProtectionScope.CurrentUser.
                return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not decrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
} /// <summary>
/// 创建bat脚本
/// </summary>
/// <param name="batName">文件名</param>
/// <param name="fileContent">文件内容</param>
/// <param name="u"></param>
private void CreationBat(string batName, string fileContent, string u = null)
{
string filepath = System.IO.Directory.GetCurrentDirectory();
string batpath = filepath + @"\" + batName;
writeBATFile(fileContent, batpath);
} /// <summary>
/// 写入文件
/// </summary>
/// <param name="fileContent">文件内容</param>
/// <param name="filePath">路径</param>
private void writeBATFile(string fileContent, string filePath)
{
if (!File.Exists(filePath))
{
FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine(fileContent);//开始写入值
sw.Close();
fs1.Close();
}
else
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
sr.WriteLine(fileContent);//开始写入值
sr.Close();
fs.Close();
}
} /// <summary>
/// 调用执行bat文件
/// </summary>
/// <param name="batName">文件名</param>
/// <param name="thisbatpath">路径</param>
private void ExecuteBat(string batName, string thisbatpath)
{
Process proc = null;
try
{
string targetDir = string.Format(thisbatpath);//this is where testChange.bat lies
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = batName;
proc.StartInfo.Arguments = string.Format("");//this is argument
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
}
} /// <summary>
/// 调用执行vbs文件
/// </summary>
/// <param name="vbsName">文件名</param>
private void ExecuteVbs(string vbsName)
{
string path = System.IO.Directory.GetCurrentDirectory() + @"\" + vbsName;
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "wscript.exe";
startInfo.Arguments = path;
Process.Start(startInfo);
} /// <summary>
/// 创建vbs文件
/// </summary>
/// <param name="vbsName">vbs文件名</param>
private void NewVbs(string vbsName)
{
string path = System.IO.Directory.GetCurrentDirectory() + @"\" + vbsName;
FileStream fsvbs = new FileStream(path, FileMode.Create, FileAccess.Write);
StreamWriter runBat = new StreamWriter(path);
runBat.WriteLine("set ws=WScript.CreateObject(\"WScript.Shell\")");
runBat.WriteLine("ws.Run\"runRdp.bat\",0");
runBat.Close();
fsvbs.Close();
}
}

OpenRemoteDesktop

调用示例

  OpenRemoteDesktop m_open = new ServicesBLL.Common.OpenRemoteDesktop();
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. 关于block元素和inline元素

    呃...这个会不会太基础了.最近在复习,所以基础知识也不能够忽略. 根据HTML 4.01 规范,其描述如下(http://www.w3.org/TR/html401/struct/global.ht ...

  2. php中错误处理机制

    php中,异常处理机制是有限的,无法自动抛出异常,必须手动进行,并且内置异常有限. php把许多异常看作错误,这样就可以把这些异常想错误一样用set_error_handler接管,进而主动抛出异常. ...

  3. mysql自定义函数收集

    代码: 查找字符串 in_string 中,存在多少个字符串 in_find_str delimiter $$ DROP FUNCTION IF EXISTS `fn_findCharCount` $ ...

  4. centos7之iptables与firewalld

    保障数据的安全性是继保障数据的可用性之后最为重要的一项工作.防火墙作为公网 与内网之间的保护屏障,在保障数据的安全性方面起着至关重要的作用. firewalld与iptables iptables f ...

  5. OpenGLES.gpus_ReturnNotPermittedKillClient

    在iOS中,使用OpenGLES不当引起的crash:“gpus_ReturnNotPermittedKillClient” https://developer.apple.com/library/i ...

  6. Codeforces 709C 模拟

    C. Letters Cyclic Shift time limit per test:1 second memory limit per test:256 megabytes input:stand ...

  7. laravel加载视图

    1.控制器 2.路由 3.视图

  8. makefile all

    all:udps udpc udps:udpserv.c    gcc -Wall -o udps udpserv.cudpc:udpclient.c    gcc -Wall -o udpc udp ...

  9. Laravel 处理 Options 请求的原理以及批处理方案

    0. 背景 在前后端分离的应用中,需要使用CORS完成跨域访问.在CORS中发送非简单请求时,前端会发一个请求方式为OPTIONS的预请求,前端只有收到服务器对这个OPTIONS请求的正确响应,才会发 ...

  10. 管理型交换机IEEE 802.1Q VLAN设置应用实例

    转载标注: IEEE802.1Q 我粗略理解为对交换机入口规则和出口规则设置,入口打上VID,设置从哪些口可以出去,并且是否带标签. 一 VLAN的概念 VLAN(Virtual Local Area ...