'how i can programmatically close rdp connection

I have an application on the server. I can connect to it using rdp, but under some circumstances(request from client, for example) I need the service to close the connection. How can I detect an existing rdp connection and close it if it has?



Solution 1:[1]

windows utility tsdiscon can do this

Process.Start(Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess
   ? Environment.ExpandEnvironmentVariables(@"%windir%\sysnative\tsdiscon.exe")
   : "tsdiscon.exe");

Solution 2:[2]

You can try with the following code. I tried at my side and it is working.

Package : using System.Diagnostics;

    Process[] processes = Process.GetProcessesByName("mstsc");
    int pid = processes[0].Id;
    Process pro = Process.GetProcessById(pid);
    pro.Kill();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hellsmenser
Solution 2 Ajay Gupta