62,025
社区成员
发帖
与我相关
我的任务
分享
[DllImport("User32.dll" , CharSet = CharSet.Auto)]
private static extern int GetWindowThreadProcessId(IntPtr hwnd , out int ID);
//End the process of xlApp
public void KillExcelProcess()
{
//get the hwnd(t) of excel, it is the entrance of memory used by excel
IntPtr t = new IntPtr(xlApp.Hwnd);
int k = 0;
//get the id(k) of process by hwnd(t)
GetWindowThreadProcessId(t , out k);
//get the reference of process(p) by id(k)
Process p = Process.GetProcessById(k);
//kill the process
p.Kill();
}
public void KillExcelProcess2(string fileName)
{
Process[] procList = Process.GetProcesses();
IntPtr t = new IntPtr(xlApp.Hwnd);
int k = 0;
//get the id(k) of process by hwnd(t)
GetWindowThreadProcessId(t, out k);
foreach (Process proc in procList)
{
if (k == proc.Id)
proc.Kill();
}
}