-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessExtension.cs
More file actions
33 lines (30 loc) · 1.03 KB
/
ProcessExtension.cs
File metadata and controls
33 lines (30 loc) · 1.03 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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace MyProcessUsage
{
internal static class ProcessExtension
{
public static string PerformanceCounterInstanceName(this Process process)
{
var matchesProcessId = new Func<string, bool>(instanceName =>
{
using (var pc = new PerformanceCounter("Process", "ID Process", instanceName, true))
{
if ((int)pc.RawValue == process.Id)
{
return true;
}
}
return false;
});
string processName = Path.GetFileNameWithoutExtension(process.ProcessName);
return new PerformanceCounterCategory("Process")
.GetInstanceNames()
.AsParallel()
.FirstOrDefault(instanceName => instanceName.StartsWith(processName)
&& matchesProcessId(instanceName));
}
}
}