欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c#实现windows远程桌面连接程序代码

时间:2021-02-15 10:20:44|栏目:.NET代码|点击:

使用winform制作windows远程桌面连接程序,windows自带了远程桌面连接,我们需要将远程桌面连接集成

到自己的winform程序,并实现管理远程主机的配置。

远程桌面核心类库

windows系统自带了远程桌面activex dll,目录:

c:\Windows\System32\mstscax.dll

此类库无法使用c#直接调用,介绍一个工具AxImp.exe

AxImp.exe

https://msdn.microsoft.com/zh-cn/library/8ccdh774(VS.80).aspx

ActiveX 控件导入程序将 ActiveX 控件的 COM 类型库中的类型定义转换为 Windows 窗体控件。

控件转换

在cmd输入以下命令

"c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\AxImp.exe" "c:\Windows\System32\mstscax.dll"

即可生成AxMSTSCLib.dll,MSTSCLib.dll

远程桌面连接核心代码

//远程连接核心方法
private AxMSTSCLib.AxMsRdpClient7 rdpc = null;
protected void OnCreateControl()
{
  rdpc = new AxMSTSCLib.AxMsRdpClient7();
  rdpc.OnDisconnected += new AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEventHandler(rdpc_OnDisconnected);
  this.Controls.Add(rdpc);
  rdpc.Dock = DockStyle.Fill;
  rdpc.BringToFront();
}

void rdpc_OnDisconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e)
{
  //处理断开连接
}


public void Disconnect()
{
  try
  {
    if (rdpc.Connected == 1)
    {
      rdpc.Disconnect();
    }
  }
  catch (Exception)
  {

  }

}

private void SetRdpClientProperties(Machine parMachine)
{
  rdpc.Server = parMachine.MachineName;
  rdpc.AdvancedSettings2.RDPPort = parMachine.Port;
  rdpc.UserName = parMachine.UserName;
  rdpc.Domain = parMachine.DomainName;
  if (parMachine.Password != "")
  {
    rdpc.AdvancedSettings5.ClearTextPassword = parMachine.Password;
  }
  rdpc.AdvancedSettings5.RedirectDrives = parMachine.ShareDiskDrives;
  rdpc.AdvancedSettings5.RedirectPrinters = parMachine.SharePrinters;
  rdpc.ColorDepth = (int)parMachine.ColorDepth;
}

public void Connect(Machine parMachine)
{
  SetRdpClientProperties(parMachine);
  rdpc.Connect();
}

//远程主机配置
[Serializable()]
public class Machine
{
  private string _RemoteDesktopConnectionName;
  public string RemoteDesktopConnectionName
  {
    get { return _RemoteDesktopConnectionName; }
    set { _RemoteDesktopConnectionName = value; }
  }

  private string _MachineName;
  public string MachineName
  {
    get { return _MachineName; }
    set { _MachineName = value; }
  }
  private string _DomainName;
  public string DomainName
  {
    get { return _DomainName; }
    set { _DomainName = value; }
  }

  private string _UserName;
  public string UserName
  {
    get { return _UserName; }
    set { _UserName = value; }
  }

  private string _Password;
  public string Password
  {
    get { return _Password; }
    set { _Password = value; }
  }

  private bool _AutoConnect;
  public bool AutoConnect
  {
    get { return _AutoConnect; }
    set { _AutoConnect = value; }
  }

  private bool _ShareDiskDrives;
  public bool ShareDiskDrives
  {
    get { return _ShareDiskDrives; }
    set { _ShareDiskDrives = value; }
  }

  private bool _SharePrinters;
  public bool SharePrinters
  {
    get { return _SharePrinters; }
    set { _SharePrinters = value; }
  }

  private bool _SavePassword;
  public bool SavePassword
  {
    get { return _SavePassword; }
    set { _SavePassword = value; }
  }

  private Colors _ColorDepth;
  public Colors ColorDepth
  {
    get { return _ColorDepth; }
    set { _ColorDepth = value; }
  }

  public int Port
  {
    get
    {
      return _Port;
    }

    set
    {
      _Port = value;
    }
  }

  private int _Port;


  public enum Colors
  {
    HighColor15 = 15,
    HighColor16 = 16,
    Color256 = 8,
    TrueColor = 24
  }
}

上一篇:asp.net c#采集需要登录页面的实现原理及代码

栏    目:.NET代码

下一篇:C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能

本文标题:c#实现windows远程桌面连接程序代码

本文地址:http://www.codeinn.net/misctech/63298.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有