。net定时关闭excel进程

public void Application_Start()
{
// 在应用程序启动时运行的代码
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(killAllProcess);

}

public void killAllProcess(object sender, EventArgs e)
{
System.Diagnostics.Process[] myPs;
myPs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in myPs)
{
if (p.Id != 0)
{
try
{
if (p.Modules != null)
if (p.Modules.Count > 0)
{
System.Diagnostics.ProcessModule pm = p.Modules[0];
if (pm.ModuleName.ToUpper().Contains("EXCEL"))
p.Kill();
}
}
catch
{

}
finally
{

}
}
}
}

posted @ 2016-12-07 16:23  .net程序狗  阅读(182)  评论(0编辑  收藏  举报