62,072
社区成员
发帖
与我相关
我的任务
分享
if (excelApp != null)
{
//Marshal.ReleaseComObject(excelApp);
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
//KillExcel(excelApp);
excelApp = null;
}
ReleaseComObject不能关闭进程private static void KillExcel(Microsoft.Office.Interop.Excel.Application theApp)
{
int id = 0;
IntPtr intptr = new IntPtr(theApp.Hwnd);
System.Diagnostics.Process p = null;
try
{
GetWindowThreadProcessId(intptr, out id);
p = System.Diagnostics.Process.GetProcessById(id);
if (p != null)
{
p.Kill();
p.WaitForExit();
p.Dispose();
}
}
catch (Exception ex)
{
}
}
p.Kill();会报拒绝访问错误 using (HostingEnvironment.Impersonate()) {}
权限是在应用程序运行账号下执行的//System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
//excelApp = null;
//System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//foreach (Process thisproc in Process.GetProcessesByName("EXCEL"))
//{
// if (!thisproc.CloseMainWindow())
// {
// thisproc.Kill();
// GC.Collect();
// }
//}
用这个方式能关掉excel进程,但是是所有的。 为什么kill函数不可以??System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
我把匿名访问禁用了,加了身份模拟 用windows认证 还是说没权限 无奈了using (HostingEnvironment.Impersonate())
{}
去调用kill怎么还是删不掉啊
const int WM_CLOSE = 0x0010, //关闭窗口
WM_QUIT = 0x0012; //退出线程消息循环
//发送消息后不等待立即返回
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, int WM_CHAR, int wParam, int lParam);
public static void CloseWindow(IntPtr hWnd)
{
//注意:单独发送WM_CLOSE消息,有些程序将一直等待用记确认保存或关闭
//再补发一条WM_QUIT消息就行了
PostMessage(hWnd, Mapper.WM_CLOSE, 0, 0);
PostMessage(hWnd, Mapper.WM_QUIT, 0, 0);
}
CloseWindow(程序主窗口句柄); //可以传process.MainWindowHandle