.net 部署IIS 在服务器无法杀掉EXCEL进程

[Win32Exception (0x80004005): 拒绝访问。]
   System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited) +1985316
   System.Diagnostics.Process.Kill() +49
   ApricotCMS.Controllers.ImportController.Kill(_Application excel) +144
   ApricotCMS.Controllers.ImportController.Family(HttpPostedFileBase file) +27340
   lambda_method(Closure , ControllerBase , Object[] ) +127
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264

  上面是堆栈信息  无法kill掉excel进程 因为权限不够

修改线程池中的Identity 

修改为管理权限就可以关闭进程了

用回收器可以搞定 

appExcel.Workbooks.Close();
appExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
appExcel = null;

GC.Collect();

用kill进程的方法很好 在本地测试完全没问题

[DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

        public  void Kill(Microsoft.Office.Interop.Excel.Application excel)
        {
            IntPtr t = new IntPtr(excel.Hwnd);   //得到这个句柄,具体作用是得到这块内存入口   

            int k = 0;
            GetWindowThreadProcessId(t, out k);   //得到本进程唯一标志k  
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);   //得到对进程k的引用  
            p.Kill();     //关闭进程k  

        } 

在服务器上就不行  提示 

Win32Exception (0x80004005): 拒绝访问

所以还是用GC回收的方式可行 测试绝对可行
原文地址:https://www.cnblogs.com/seanjack/p/7478613.html