0

I am writing a program that continuously checks if rdp is running. If not running than it will send an email notification. Could someone tell me how to code that part which check if mstsc process is running.

using System.Diagnostics;
using System.Net.Mail;
using System.Net;

var from = "name@mail.com";
var to = "name2@mail2.com";
var subject = "Test mail";
var body = "Test body";

var username = "mailserver@mail.com"; // get from Mailtrap
var password = "password"; // get from Mailtrap

var host = "mailserver";
var port = portnumber;

var client = new SmtpClient(host, port)
{
    Credentials = new NetworkCredential(username, password),
    EnableSsl = false
};

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

client.Send(from, to, subject, body);
Lennart
  • 9,657
  • 16
  • 68
  • 84
SimK
  • 5
  • 3
  • 1
    What do you mean by 'rdp is running'? The Remote Desktop Services service is running? Or you're running in a remote session? Or there are any remote sessions active? – stuartd Jan 31 '23 at 13:18
  • So `Process.GetProcessesByName` returns an array of the processes that are running with that name. If that array is not null, then the process is running... – Ron Beyer Jan 31 '23 at 13:20
  • Take a look at this post, may provide some guidance/examples: [How can I know if a process is running?](https://stackoverflow.com/questions/262280/how-can-i-know-if-a-process-is-running) – Swemoph Jan 31 '23 at 13:22
  • remote desktop connection must be always running, MSTSC process is rdp process. I need it to continuosly check if mstsc is running. If not than send an email. I need to find out how to continue checking if the variable processes is null – SimK Jan 31 '23 at 13:41
  • I need to check if there is an open rdp session running. checking mstsc wont work. – SimK Jan 31 '23 at 13:56
  • if (pro is null) client.Send(from, to, subject, body); else Console.WriteLine("RDP IS RUNNING"); if I run when rdp is off than visual studio throws an error System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' – SimK Jan 31 '23 at 14:05
  • RE your last comment: Because `Process.GetProcessesByName("mstsc");` is returning nothing when Remote Desktop is not running, going `int pid = processes[0].Id;` will throw an exception as there's no element at position 0. You could use the `Length` property on your processes var to check instead. – Swemoph Jan 31 '23 at 14:29
  • I tried int pid = processes[0].length; but it shows that there is no such property – SimK Feb 01 '23 at 11:01
  • Ultimate goal is to know if an rdp session to the webserver is running. If rdp is closed than our website is offline. Is there a method that checks for an open rdp session? – SimK Feb 01 '23 at 11:37
  • Can someone tell me how to make the app continuously check if rdp is open or not. What statement should I use? Currently it checks once and it stops running. – SimK Feb 01 '23 at 12:33

0 Answers0