elowell elowell - 2 months ago 19
C# Question

How can you prevent multiple instances of RDP from opening using Process.Start?

I am simply trying to open the Windows RDP application through a button click event in my C# application, but what I get is 4 or 5 instances of RDP on a single click. I was wondering if this is a result of having the code in a button click event. I've researched Mutex, but it doesn't seem to be what I'm looking for in this particular case. Any ideas?

private void btnRemote_Click(object sender, EventArgs e)
{
string rdcSupport = "C:\\Windows\\System32\\mstsc.exe";

try
{
procRDC.StartInfo.FileName = rdcSupport;
procRDC.Start();
procRDC.WaitForInputIdle();
SendKeys.Send("support_server1");
SendKeys.Send("{ENTER}");
}
catch
{
Console.WriteLine("Failed to open...");
}
}

Answer

May be SendKeys doing some mess. I would recomend using MSTSC parameters:

try
{
    Process procRDC = Process.Start(rdcSupport, "/v:support_server1");
}
catch
{
    Console.WriteLine("Failed to open...");
}

You don't show how you create/initialize procRDP.

See MSTSC help: https://technet.microsoft.com/en-us/library/cc753907(v=ws.11).aspx

Comments