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?
Asked
Active
Viewed 273 times
2 Answers
1
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();

Ajay Gupta
- 703
- 5
- 11
-
nope, processes.Lenght is 0 – hellsmenser Mar 05 '22 at 09:52
-
then process mstsc is not running or run you program as an admin. – Ajay Gupta Mar 05 '22 at 09:56
-
i think mstsc is not running, but i still connect to my remote pc, idk how it work :/ – hellsmenser Mar 05 '22 at 10:05
-
could you please share the application name? By which application or software are you using to connect remote? – Ajay Gupta Mar 05 '22 at 10:09
-
mstsc.exe but this is on my computer, I need to close the connection from the remote computer – hellsmenser Mar 05 '22 at 10:12
1
windows utility tsdiscon can do this
Process.Start(Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess
? Environment.ExpandEnvironmentVariables(@"%windir%\sysnative\tsdiscon.exe")
: "tsdiscon.exe");

hellsmenser
- 39
- 7