运行期间关闭excell文件 [英] Close excell file during run
本文介绍了运行期间关闭excell文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#编程期间打开一个excel.exe我想确保用户关闭excel文件
我使用这段代码,但它不起作用你的想法?
Process [] prs = Process.GetProcesses();
foreach(流程pr in prs)
{
if(pr.ProcessName ==EXCEL.EXE * 32)
{
pr.Kill();
}
}
解决方案
一个非常一般的答案:而不是询问这样的问题,在调试器下运行你的代码,看看是什么进程名称。你可以在一分钟内解决你的问题。
但这个想法并不好:任何过程都可能意外地有这个名字。您不能保证这是您正在寻找的过程;进程名称不是唯一的。并且定期杀死进程的整个想法非常糟糕。你需要解释你的最终目标。我几乎可以肯定,我们可以建议你一些文明的方法解决你的问题。杀戮不是一件温和的事情,不能给你带来好的结果。
-SA
< pre lang =cs> foreach (处理pr in prs)
{
MessageBox.Show(pr.ProcessName);
if (pr.ProcessName == EXCEL)
{
pr.Kill();
}
}
i open a excel.exe during C# programing i want to be sure the user close excel file
i use this code but it dose not work what you think ?
Process[] prs = Process.GetProcesses(); foreach (Process pr in prs) { if (pr.ProcessName == "EXCEL.EXE *32") { pr.Kill(); } }
解决方案
A very general answer: instead of asking such question, run your code under debugger and see what process names are. You would solve your problem in a minute.
But the idea is not good: any process can accidentally have this name. You cannot guarantee that this is the process you are looking for; process names are not unique. And the whole idea of killing a process in a regular basis is really bad. You need to explain your ultimate goal. I''m almost sure we could suggest you some civilized approach to your problem. Killing is not a gentle thing and cannot give you good results.
—SA
foreach (Process pr in prs) { MessageBox.Show(pr.ProcessName); if (pr.ProcessName == "EXCEL") { pr.Kill(); } }
这篇关于运行期间关闭excell文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文