C#实现计算机远程操作
1、windows远程桌面连接
在 Visual Studio 中添加引用,添加 COM 中的 “Microsoft Terminal Services Control Type Library”,这个过程将会把 MSTSCLib.dll 添加到项目中。
在工具栏中添加控件,在工具栏上右键选择“选择项…”,将 COM 组件中的 “Microsoft Terminal Services Client Control” 选中,确定后就会有相应的控件出现,然后拖拽就可以使用了。
代码的实现
axMsTscAxNotSafeForScripting1 为控件的名称,str_computer 为 IP 地址、str_userName 为用户名、str_password 为密码。
using MSTSCLib;
this.axMsRdpClient71.Server = “192.168.4.188”;
this.axMsRdpClient71.UserName = “Administrator”;
IMsTscNonScriptable secured = (IMsTscNonScriptable)this.axMsRdpClient71.GetOcx();
secured.ClearTextPassword = “123456654321”;
this.axMsRdpClient71.Connect();
2、对于类Linux远程操作
使用到包:SharpSSH-1.1.1.13.bin
引用Tamir.SharpSsh
核心代码:
public static string RunSSHCommands(String host, String username, String password, String[] commands)
{
string output = null;
if (commands == null || commands.Length == 0)
return null;
try
{
SshExec exec = new SshExec(host, username);
exec.Password = password;
exec.Connect();
foreach (String command in commands)
{
if (command == null || command.Trim().Length == 0) continue;
output += exec.RunCommand(command);
}
exec.Close();
return output;
}
catch (Exception e)
{
output = e.Message;
return output;
}
}
登录服务器执行:
string[] comstr = new string[1];
comstr[0] = this.textBox1.Text;
this.label1.Text = RunSSHCommands(“192.168.32.243”, “root”, “你机器的密码”, comstr);
程序的源程序下载:源码与资源下载