How to tell if a process is running on windows using c # or .bat?

0

I'm trying to create an application that runs in the background and monitor some service to see if it's running or could someone give me a light?

    
asked by anonymous 10.10.2016 / 03:15

2 answers

2

Using C #, you can use the Process class. See the example below where I check if the notepad is open.

Process[] processos = Process.GetProcessesByName("notepad");

if (pname.Length == 0)
    Console.WriteLine("Notepad NÃO está sendo executado");
else
    Console.WriteLine("Notepad está sendo executado");

It is also possible to recover all processes that are running

Process[] processos = Process.GetProcesses();

foreach(var p in processos)
{
    Console.WriteLine($"Processo: {p.ProcessName} ID: {p.Id}");
}
    
10.10.2016 / 03:19
0

In C #, you only use the Windows Diagnostics library.

using System.Diagnostics;

Process[] processo = Process.GetProcessesByName("nomeDoProcesso");
if (processo.Length > 0) Console.WriteLine("Seu processo está rodando!");

Note however, that most processes have ".exe" at the end, but you should not use this. For example, in the task manager is: "firefox.exe", but in GetProcessesByName () you should only use "firefox".

Process.GetProcessesByName("firefox");

Already in some .bat file:

@ECHO OFF
TASKLIST /NH /FI "IMAGENAME eq %1" | FIND /I "%1" > NUL
IF %ERRORLEVEL%==0 echo PROCESSO RODANDO

Then just run the .bat with the process name, and if any process is found it will be printed in cmd.

C:\>teste.bat firefox.exe
    
10.10.2016 / 04:16