728x90
728x170
■ Process 클래스를 사용해 프로세스 소유자명을 구하는 방법을 보여준다.
▶ Process 클래스 : 프로세스 소유자명 구하기 예제 (C#)
using System;
using System.Diagnostics;
Process process = Process.GetCurrentProcess();
string ownerName = GetProcessOwnerName(process);
Console.WriteLine($"프로세스명 : {process.ProcessName}");
Console.WriteLine($"소유자명 : {ownerName }");
▶ Process 클래스 : 프로세스 소유자명 구하기 (C#)
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
#region 프로세스 소유자명 구하기 - GetProcessOwnerName(process)
/// <summary>
/// 프로세스 소유자명 구하기
/// </summary>
/// <param name="process">프로세스</param>
/// <returns>프로세스 소유자명</returns>
public string GetProcessOwnerName(Process process)
{
IntPtr processHandle = IntPtr.Zero;
try
{
OpenProcessToken(process.Handle, 8, out processHandle);
WindowsIdentity windowsIdentity = new WindowsIdentity(processHandle);
string ownerName = windowsIdentity.Name;
return ownerName.Contains(@"\") ? ownerName.Substring(ownerName.IndexOf(@"\") + 1) : ownerName;
}
catch
{
return null;
}
finally
{
if(processHandle != IntPtr.Zero)
{
CloseHandle(processHandle);
}
}
}
#endregion
#region 프로세스 토큰 열기 - OpenProcessToken(processHandle, desiredAccess, tokenHandle)
/// <summary>
/// 프로세스 토큰 열기
/// </summary>
/// <param name="processHandle">프로세스 핸들</param>
/// <param name="desiredAccess">희망 액세스</param>
/// <param name="tokenHandle">토큰 핸들</param>
/// <returns>처리 결과</returns>
[DllImport("advapi32", SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle);
#endregion
#region 핸들 닫기 - CloseHandle(objectHandle)
/// <summary>
/// 핸들 닫기
/// </summary>
/// <param name="objectHandle">객체 핸들</param>
/// <returns>처리 결과</returns>
[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr objectHandle);
#endregion
728x90
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] 최대 공약수를 사용해 정수 비율 구하기 (0) | 2021.08.20 |
---|---|
[C#/COMMON] 최대 공약수 구하기 (0) | 2021.08.20 |
[C#/COMMON] MD5 클래스 : ComputeHash 메소드를 사용해 MD5 해시값 구하기 (0) | 2021.08.20 |
[C#/COMMON] List<T> 클래스 : RemoveAll 메소드를 사용해 조건에 해당하는 요소 삭제하기 (0) | 2021.08.20 |
[C#/COMMON] Process 클래스 : 프로세스명과 사용자명으로 프로세스 죽이기 (0) | 2021.08.20 |
[C#/COMMON] TcpListener 클래스 : Pending 메소드를 사용해 신규 연결시 기존 연결 버리기 (0) | 2021.08.20 |
[C#/COMMON] Regex 클래스 : Replace 정적 메소드를 사용해 공백 문자열 제거하기 (0) | 2021.08.20 |
[C#/COMMON] 특정 타입의 인터페이스 구현 여부 구하기 (0) | 2021.08.19 |
[C#/COMMON] StackFrame 클래스 : GetMethod 메소드를 사용해 이전 실행 메소드 구하기 (0) | 2021.08.19 |
[C#/COMMON/.NET5] dotnet publish 명령 : 단일 exe 파일 생성하기 (0) | 2021.08.19 |