■ Process 클래스를 사용해 프로세스 소유자명을 구하는 방법을 보여준다.
▶ Process 클래스 : 프로세스 소유자명 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 |
using System; using System.Diagnostics; Process process = Process.GetCurrentProcess(); string ownerName = GetProcessOwnerName(process); Console.WriteLine($"프로세스명 : {process.ProcessName}"); Console.WriteLine($"소유자명 : {ownerName }"); |
▶ Process 클래스 : 프로세스 소유자명 구하기 (C#)
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
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 |