3

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.

DotNetSpartan
  • 931
  • 4
  • 20
  • 41

2 Answers2

2

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.

Reference for ShowWindow function

ivcubr
  • 1,988
  • 9
  • 20
  • 28
  • I tried your solution but it still not minimizes the RDP mirror window. – DotNetSpartan Jul 18 '19 at 14:44
  • @user42067 what do you mean by RDP mirror window? Did you add something to properly delay during the time it takes for the new process to be created? – ivcubr Jul 18 '19 at 14:45
  • The window that launches and let the user see the computer. I mean the window that has three symbols(Network symbol, Lock, pin/unpin) and computer name at the top. I want to minimize that window through C#. – DotNetSpartan Jul 18 '19 at 14:50
  • @user42067 did you wait sufficiently between the end of `proc.Start();` and the next line. See my note about needing to wait for the new process to be created – ivcubr Jul 18 '19 at 15:04
  • @user42067 that certainly works, in my environment, `Thread.Sleep(10000);` is plenty. I did not specify a way to wait in the answer as I do not know enough about your application. Another way would be to monitor the number of `mstsc` processes and wait for a new one to be created. This would be IMO the cleanest solution, but it is up to your implementation – ivcubr Jul 18 '19 at 15:18
-1

Use windows style, this works.

    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);
Sergio
  • 138
  • 7
  • 1
    The process starts minimized and then works and responds to what you asked for , if you do NOT have the saved credentials, you open a window where you are asked to enter them, and in this case you are right @ivcubr. Anyway, I just don't understand why you give a negative score to someone who tried to help you. – Sergio Jul 18 '19 at 14:09
  • Sorry for the negative vote, Please edit your answer so that I can vote it as positive. Your answer was of no use to me though. But thanks for the help. – DotNetSpartan Jul 18 '19 at 14:41