The below piece of code makes me to establish a remote desktop connection with the computer machine through 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();
I want to minimize the RDC window (The mirrow window) once it is launched successfully. Is there any way to do it through C# here ?
This is what I tried, but it makes no difference:
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Any help will be much appreciated.
You can use the the ShowWindow
function from user32.dll
. Add the following import to your program. You will need a reference to using System.Runtime.InteropServices;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
What you already have to start RDP will work as you have it, but then you will need to get the new mstsc
process that is created after the remote desktop opens up. The original process you started exits after proc.Start()
. Using the code below will get you the first mstsc
process. NOTE: you should select better than just taking the first if you have more than one RDP window open.
Process process = Process.GetProcessesByName("mstsc").First();
Then call the ShowWindow
method as shown below with SW_MINIMIZE = 6
ShowWindow(process.MainWindowHandle, SW_MINIMIZE);
The full solution becomes:
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);
}
NOTE: the answer from @Sergio will work, but it will minimize the initial process that is created. If you need to enter credentials, I don't think that is the correct approach.