gpt4 book ai didi

c# - 如何通过 C# 最小化远程桌面连接 (RDC) 窗口?

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:33 26 4
gpt4 key购买 nike

下面这段代码让我通过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;

任何帮助将不胜感激。

最佳答案

您可以使用 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

关于c# - 如何通过 C# 最小化远程桌面连接 (RDC) 窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57094775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com