c#使用多个远程桌面连接

写在前面:使用Windows Server服务器的朋友可以找到连接多个远程桌面的功能,但使用Xp操作系统就不行了。远程桌面功能就是使用mstsc这个文件的。在system32文件夹可以找到MSTSCAX.DLL这个动态连接库,这是实现远程连接的主要组件。下载介绍一个用C#编写远程桌面功能,可以连接多个Windows服务器。

源代码
http://www.codeproject.com/KB/cs/RemoteDesktopClient.aspx
你可以到上面的网址了解到更加详细的信息

效果图
下图的左边是一个远程列表,右边是MDI显示区域,可以显示多个远程界面。
 
下图是这个工具的说明,是使用Vs2008 RTM开发的,.Net版本是3.5Sp1。
 

源代码分析
相信大家对这个程序最感兴趣的地方是连接mstsc动态链接库的代码。
其中rdpClient 是AxMSTSCLib.AxMsRdpClient 类型对象。
声明如下:
public AxMSTSCLib.AxMsRdpClient rdpClient;
连接远程

        public void Connect()
        {
            Status("Starting ...");
            rdpClient.Connect();
        }


断开远程连接

        public void Disconnect()
        {
            Status("Disconnecting ...");
            rdpClient.DisconnectedText = "Disconnected";

            if (rdpClient.Connected != 0)
            {
                rdpClient.Disconnect();
            }
        }


重新连接远程对象
连接的过程中需要指定服务器,用户名,密码等等。

        public void Reconnect(bool hasChanges, bool isFitToWindow)
        {
            Disconnect();

            Status("Waiting for the server to properly disconnect ...");

            // wait for the server to properly disconnect
            while (rdpClient.Connected != 0)
            {
                System.Threading.Thread.Sleep(1000);
                Application.DoEvents();
            }

            Status("Reconnecting ...");

            if (hasChanges)
            {
                rdpClient.Server = this._ss.Server;
                rdpClient.UserName = this._ss.Username;
                rdpClient.AdvancedSettings2.ClearTextPassword = this._ss.Password;
                rdpClient.ColorDepth = this._ss.ColorDepth;

                this._isFitToWindow = isFitToWindow;

                if (isFitToWindow)
                {
                    rdpClient.DesktopWidth = this.rdpClient.Width;
                    rdpClient.DesktopHeight = this.rdpClient.Height;
                }
                else
                {
                    rdpClient.DesktopWidth = this._ss.DesktopWidth;
                    rdpClient.DesktopHeight = this._ss.DesktopHeight;
                }

                rdpClient.FullScreen = this._ss.Fullscreen;
            }

            Connect();
        }


在连接的过程中,会使用大量Com接口函数。
这一程序,可以作为.Net实现远程桌面的重要参考。

posted @ 2010-04-30 17:05  gllg  阅读(1192)  评论(1编辑  收藏  举报