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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Input;
 
namespace Test {
    class Program {
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();
 
        [DllImport("user32.dll")]
        public static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
 
        static void Main(string[] args)
        {
            int a = 0;
            while (a++ < 10) {
                Thread.Sleep(1000);
 
                IntPtr hwnd = GetForegroundWindow();
                uint pid;
                if (hwnd == nullcontinue;
 
                GetWindowThreadProcessId(hwnd, out pid);
                Process CurProc = Process.GetProcessById((int)pid);
                var name = CurProc.MainWindowTitle;
                Console.WriteLine(name);
            }
        }
    }
}
cs


반응형