■ Process 클래스를 사용해 메인 모듈의 파일 경로를 구하는 방법을 보여준다.
▶ ProcessHelper.cs
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; namespace TestProject { /// <summary> /// 프로세스 헬퍼 /// </summary> public static class ProcessHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 전체 프로세스 이미지명 질의하기 - QueryFullProcessImageName(processHandle, flag, filePathStringBuilder, filePathLength) /// <summary> /// 전체 프로세스 이미지명 질의하기 /// </summary> /// <param name="processHandle">프로세스 핸들</param> /// <param name="flag">플래그</param> /// <param name="filePathStringBuilder">파일 경로 문자열 빌더</param> /// <param name="filePathLength">파일 경로 길이</param> /// <returns>처리 결과</returns> [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)] private static extern bool QueryFullProcessImageName ( IntPtr processHandle, uint flag, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder filePathStringBuilder, ref uint filePathLength ); #endregion #region 프로세스 열기 - OpenProcess(desiredAccess, inheritHandle, processID) /// <summary> /// 프로세스 열기 /// </summary> /// <param name="desiredAccess">희망 액세스</param> /// <param name="inheritHandle">핸들 상속 여부</param> /// <param name="processID">프로세스 ID</param> /// <returns>프로세스 핸들</returns> [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern IntPtr OpenProcess(int desiredAccess, bool inheritHandle, int processID); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 메인 모듈 파일 경로 구하기 - GetMainModuleFilePath(process, filePathLength) /// <summary> /// 메인 모듈 파일 경로 구하기 /// </summary> /// <param name="process">프로세스</param> /// <param name="filePathLength">파일 경로 길이</param> /// <returns>메인 모듈 파일 경로</returns> public static string GetMainModuleFilePath(Process process, int filePathLength = 1024) { if(process == null) { return string.Empty; } int queryLimitedInformation = 0x1000; IntPtr processHandle = OpenProcess(queryLimitedInformation, false, process.Id); if(processHandle == IntPtr.Zero) { return string.Empty; } StringBuilder stringBuilder = new StringBuilder(filePathLength); uint bufferLength = (uint)stringBuilder.Capacity; return QueryFullProcessImageName(processHandle, 0, stringBuilder, ref bufferLength) ? stringBuilder.ToString() : string.Empty; } #endregion } } |
▶ Program.cs
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; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Process process = Process.GetCurrentProcess(); string filePath = ProcessHelper.GetMainModuleFilePath(process); Console.WriteLine(filePath); } #endregion } } |