如何通过 C# 最小化远程桌面连接 (RDC) window?

How to minimize the remote desktop connection (RDC) window through C#?

下面的一段代码让我通过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 window (The mirrow window) 启动成功后最小化。有什么办法可以通过 C# 来完成吗?

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

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

任何帮助将不胜感激。

使用 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);

您可以使用 user32.dll 中的 ShowWindow 功能。将以下导入添加到您的程序中。您需要参考 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 window 打开,您应该 select 比只使用第一个更好。

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

然后用SW_MINIMIZE = 6

调用如下所示的ShowWindow方法
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 的答案会起作用,但它会最小化创建的初始进程。如果您需要输入凭据,我认为这不是正确的方法。

Reference for ShowWindow function