日期:2014-05-17  浏览次数:20628 次

Win32Exception (0x80004005): 拒绝访问。
ASP.Net + C# + SQLServer 2005 + IIS 5.1 服务器环境 XP SP3
在我自己的笔记本环境中调试都是好的,到服务器上部署就出问题了,在网上查了一整天的解决方案,大致问题所在也了解了,应该是杀进程的时候权限不够,但郁闷的是网上所有的解决方案都试过了还是没用。

报错部分代码:
C# code

        [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();
        }



以下是我试过但无效的解决方案:
解决方案1:
修改 C:\Windows\Microsoft.Net\framework\v2.0.50727\Config\machine.config 文件
<processModel userName="machine" 改成 "system"/>

解决方案2:
用户权限设置不当引起的
一般通过在WEB站点的目录安全性中编辑“匿名访问和验证控制”的验证方法改为,ASPNET用户,就可以解决这个问题了。
这里我设置了ASPNET和Administrator以及IUSR_机器名以及对应密码都没有用

解决方案3:
原因: 用户没有结束Excel进程的权限。解决方案:
1.在WEBCONFIG中使用
<identity impersonate="true" userName="Administrator" password="admin"/>
放在<system.web>标签中
2.或者将ASP.NET设为管理员权限(IIS5及以下),或者NETWORKSERVICE(IIS6)

解决方案4:
控制面板-管理工具-计算机管理-本地用户和组-组-双击“Administrators”-
向其中添加ASPNET或"IIS网站属性中匿名访问时用到的帐号密码"
最省事的是添加everyone

------解决方案--------------------
解决了?~想表达什么?
------解决方案--------------------
你的权限设置都没有问题,问题在代码里面,从Id获取的进程对象权限很低,具体机制我也不是很清楚,但你需要换一种方式获得进程,再根据Id来判断,这样进程对象权限比较高,从而可以删除

C# code

        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();
            }
        }