【发布时间】:2026-01-17 04:05:02
【问题描述】:
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
DialogResult result = DialogResult.None;
if (isExcelIsRunning())
{
result = MessageBox.Show("Excel is still running. Please save your work and close Excel, and then click \"Retry\" button to continue with installation.", "", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
}
while (result == DialogResult.Retry)
{
if (!isExcelIsRunning())
{
break;
}
}
}
//check if excel is currently running
private bool isExcelIsRunning()
{
bool flag = false;
Process[] xlProc = Process.GetProcessesByName("excel");
if (xlProc != null)
flag = true;
return flag;
}
以上是我将要用于我的安装程序类的代码。
我想在这里实现的是,我需要安装程序在安装过程中检查 Excel 当前是否正在运行。如果它正在运行,那么在安装开始时应该会弹出一条消息,提醒用户关闭 Excel,并且安装程序应该暂停,直到在进程列表中不再找到 Excel 实例。
回到代码,我不认为while 块是完全正确的,因为如果同时 Excel 仍在运行,它可能会在用户单击“重试”按钮后导致无限循环。
那么更好的方法是什么?有人可以看看上面的代码,看看我可以改进吗?
【问题讨论】:
-
为什么excel不能运行?
-
因为是Excel插件安装程序,我觉得安装插件的时候如果能有一个干净的环境就好了。
-
这是有道理的。尽管这里的问题是您可能应该使用 excel 运行进行测试。毕竟,在您的预安装检查之后,没有什么可以阻止用户再次启动它。
标签: c# .net windows-installer