3

下面的一段代码让我通过 mstsc.exe 与计算机建立远程桌面连接。

 string ipAddress = "XXX.XX.XXX.XXX" // IP Address of other machine
 System.Diagnostics.Process proc = new System.Diagnostics.Process();
 proc.StartInfo.UseShellExecute = true;
 proc.StartInfo.FileName = "mstsc.exe";
 proc.StartInfo.Arguments = "/v:" + ipAddress ;    
 proc.Start();

一旦成功启动,我想最小化 RDC 窗口(镜像窗口)。有什么方法可以通过 C# 来实现吗?

这是我尝试过的,但没有区别:

proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

任何帮助都感激不尽。

4

2 回答 2

2

您可以使用 中的ShowWindow功能user32.dll。将以下导入添加到您的程序中。您将需要参考using System.Runtime.InteropServices;

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

您已经必须启动 RDP 的内容将照常工作,但随后您将需要获取mstsc在远程桌面打开后创建的新进程。您开始的原始进程在proc.Start(). 使用下面的代码将为您提供第一个mstsc过程。注意:如果您打开了多个 RDP 窗口,您应该选择比只选择第一个更好的选择。

Process process = Process.GetProcessesByName("mstsc").First();

然后调用ShowWindow如下所示的方法SW_MINIMIZE = 6

ShowWindow(process.MainWindowHandle, SW_MINIMIZE);

完整的解决方案变为:

private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

static void Main(string[] args) {
    string ipAddress = "xxx.xxx.xxx.xxx";
    Process proc = new Process();
    proc.StartInfo.UseShellExecute = true;
    proc.StartInfo.FileName = "mstsc.exe";
    proc.StartInfo.Arguments = "/v:" + ipAddress ;    
    proc.Start();

    // NOTE: add some kind of delay to wait for the new process to be created.

    Process process = Process.GetProcessesByName("mstsc").First();

    ShowWindow(process.MainWindowHandle, SW_MINIMIZE);
}

注意:@Sergio 的回答将起作用,但它将最小化创建的初始过程。如果您需要输入凭据,我认为这不是正确的方法。

ShowWindow功能参考

于 2019-07-18T13:09:32.740 回答
-1

使用windows风格,这行得通。

    string ipAddress = "xxx.xx.xxx.xxx"; // IP Address of other machine
    ProcessStartInfo p = new ProcessStartInfo("mstsc.exe");
    p.UseShellExecute = true;
    p.Arguments = "/v:" + ipAddress;
    p.WindowStyle = ProcessWindowStyle.Minimized;
    Process.Start(p);
于 2019-07-18T13:07:01.303 回答