■ PerformanceCounter 클래스 : 프로세스명으로 인스턴스명 딕셔너리 구하기 예제
------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
Dictionary<int, string> dictionary = GetInstanceNameDictionary("notepad");
foreach(KeyValuePair<int, string> keyValuePair in dictionary)
{
int processID = keyValuePair.Key;
string instanceName = keyValuePair.Value;
Console.WriteLine("프로세스 ID : {0}, 인스턴스명 : {1}", processID, instanceName);
}
------------------------------------------------------------------------------------------------------------------------
■ PerformanceCounter 클래스 : 프로세스명으로 인스턴스명 딕셔너리 구하기
------------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Diagnostics;
#region 인스턴스명 딕셔너리 구하기 - GetInstanceNameDictionary(processName)
/// <summary>
/// 인스턴스명 딕셔너리 구하기
/// </summary>
/// <param name="processName">프로세스명</param>
/// <returns>인스턴스명 딕셔너리</returns>
public Dictionary<int, string> GetInstanceNameDictionary(string processName)
{
Dictionary<int, string> dictionary = new Dictionary<int, string>();
PerformanceCounterCategory category = new PerformanceCounterCategory("Process");
string[] instanceNameArray = category.GetInstanceNames();
foreach(string instanceName in instanceNameArray)
{
if(instanceName.StartsWith(processName))
{
using(PerformanceCounter counter = new PerformanceCounter("Process", "ID Process", instanceName, true))
{
int processID = (int)counter.RawValue;
dictionary.Add(processID, instanceName);
}
}
}
return dictionary;
}
#endregion
------------------------------------------------------------------------------------------------------------------------
'C# > Common' 카테고리의 다른 글
[C#/COMMON] IDataReader 인터페이스 : 레코드를 객체로 변환하기 (0) | 2019.09.20 |
---|---|
[C#/COMMON] PropertyInfo 클래스 : 타입의 속성 정보 배열 구하기 (0) | 2019.09.20 |
[C#/COMMON] 프로세스 CPU 사용률 제한하기 (0) | 2019.09.04 |
[C#/COMMON] 프로세스 CPU 사용률 제한하기 (0) | 2019.09.04 |
[C#/COMMON] PerformanceCounter 클래스 : 인스턴스명으로 프로세스 CPU 사용률 구하기 (0) | 2019.09.04 |
[C#/COMMON] PerformanceCounter 클래스 : 프로세스명으로 인스턴스명 딕셔너리 구하기 (0) | 2019.09.04 |
[C#/COMMON] SystemEvents 클래스 : SessionSwitch 정적 이벤트를 사용해 세션 잠금/해제 처리하기 (0) | 2019.08.30 |
[C#/COMMON] Array 클래스 : Resize 정적 메소드를 사용해 배열 크기 변경하기 (0) | 2019.08.30 |
[C#/COMMON] Marshal 클래스 : 구조체 바이트 배열/객체 구하기 (0) | 2019.08.29 |
[C#/COMMON] Marshal 클래스 : 비관리 메모리 할당하기 (0) | 2019.08.29 |
[C#/COMMON] 리스트 데이터 지우기 (0) | 2019.08.27 |
댓글을 달아 주세요