IT/C#

[C#] 코드 상에서 사용한 Excel Process ID 확인하여 프로세스 죽이기

Ella.J 2024. 5. 28. 16:10
728x90
반응형

 

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 Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Diagnostics;
 
namespace TEST
{
    class ExcelSave
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint IpdwProcessId);
        
        static Excel.Application excelApp = null;
        static Excel.Workbook wb = null;
        static Excel._Worksheet ws = null;
        
        //...중간 생략...
        
        public static void ProcessKill()
        {
            uint processId = 0;
            GetWindowThreadProcessId(new IntPtr(excelApp.Hwnd), out processId);
            excelApp.Quit();
            if (processId != 0)
            {
                Process excelProcess = Process.GetProcessById((int)processId);
                excelProcess.CloseMainWindow();
                excelProcess.Refresh();
                excelProcess.Kill();
            }
        }
    }
}
cs

 

728x90
반응형