forked from antonpup/Aurora
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathProcessUtils.cs
More file actions
54 lines (48 loc) · 1.34 KB
/
ProcessUtils.cs
File metadata and controls
54 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
namespace AuroraDeviceManager.Utils;
public static class ProcessUtils
{
public static bool IsProcessRunning(string processName)
{
if (string.IsNullOrWhiteSpace(processName))
{
return false;
}
var normalizedName = Path.GetFileNameWithoutExtension(processName).Trim();
if (string.IsNullOrEmpty(normalizedName))
{
normalizedName = processName.Trim();
}
try
{
var processesByName = Process.GetProcessesByName(normalizedName);
if (processesByName.Length > 0)
{
return true;
}
}
catch
{
// ignore and fall back to service lookup
}
try
{
using var service = new ServiceController(normalizedName);
return service.Status is ServiceControllerStatus.Running
or ServiceControllerStatus.StartPending
or ServiceControllerStatus.ContinuePending;
}
catch (InvalidOperationException)
{
// service not found
}
catch (Win32Exception)
{
// access denied or service not accessible
}
return false;
}
}