您可以使用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 窗口,您应该选择比只选择第一个更好的选择。
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