【发布时间】:2021-09-03 20:22:11
【问题描述】:
我需要使用 C# 构建一个远程桌面客户端应用程序,该应用程序建立与远程 Windows Server 的连接,然后以编程方式启动远程 PC 的一些服务。
重要的是,当我登录时,服务器端的桌面环境存在,因为我要启动的服务会使用它,但在客户端我不想要任何 Windows 窗体容器,因为我想要动态创建这些会话。
为了更好地理解这个问题,假设我想使用控制台应用程序建立远程桌面连接。 关键是,在客户端我不需要任何 GUI,但主机端的服务需要窗口、鼠标、Internet Explorer 等 UI 句柄。
到目前为止,我尝试使用 MSTSClib 创建 RdpClient,如 here 所述,但这没有帮助,因为它使用了依赖于 Windows 窗体的 AxHost。
关于这是否可能的任何想法,我该如何实现?
更新:
试过这个:
using System;
using AxMSTSCLib;
using System.Threading;
using System.Windows.Forms;
namespace RDConsole
{
class Program
{
static void Main(string[] args)
{
var thread = new Thread(() =>
{
var rdp = new AxMsRdpClient9NotSafeForScripting();
rdp.CreateControl();
rdp.OnConnecting += (s, e) => { Console.WriteLine("connecting"); };
rdp.Server = "xxx.xxx.xxx.xxx";
rdp.UserName = "Administrator";
rdp.AdvancedSettings9.AuthenticationLevel = 2;
rdp.AdvancedSettings9.ClearTextPassword = "xxxxxxxxxx";
rdp.Connect();
Console.ReadKey();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
Console.ReadKey();
}
}
}
但我得到一个空引用异常
"Object reference not set to an instance of an object.
【问题讨论】:
-
到目前为止你尝试了什么?
-
我尝试使用 MSTSC.lib 来创建一个 RdpClient,如此处所述 codeproject.com/Articles/43705/Remote-Desktop-using-C-NET,但这并没有帮助,因为它使用了依赖于 Windows 窗体的 AxHost。跨度>
-
请将您尝试过的内容添加到您最初的问题中。您可能还想看看this article
-
@LorneCash 是的,看看这个stackoverflow.com/questions/37310418/…。当我完成这个项目时,我会发布一个更详细的答案,但现在这会对你有所帮助。请记住,您仍然需要手动引用 windows 窗体,并创建一个您将在其上分配 rdpclient 的窗体。在我的例子中,我开发了一个实现 rdp 连接的 Windows 服务,因此不会绘制任何窗体,并通过 WCF 与之通信。
-
@LorneCash 答案终于发布了,尽管我相信你现在已经找到了解决方法
标签: c# visual-studio remote-desktop terminal-services