첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
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
그리드형(광고전용)
Posted by icodebroker
,