首页 > Web开发 > 详细

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

时间:2017-09-05 15:13:28      阅读:806      评论:0      收藏:0      [点我收藏+]
[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回收的方式可行 测试绝对可行

 

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

原文:http://www.cnblogs.com/seanjack/p/7478613.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!