如何在应用程序运行时阻止卸载?

时间:2013-10-13 20:38:34

标签: visual-studio-2010 setup-deployment

我想在应用程序运行时停止卸载它。我想通过visual studio设置和部署来实现。 我在安装程序类中添加了以下代码,然后在操作列表中调用,但它无法正常工作。

 public static bool ApplicationIsBusy()
    {
        Process application = null;
        foreach (var process in Process.GetProcesses())
        {
            if (!process.ProcessName.Contains("applicationname")) continue;
            application = process;
            break;
        }

        if (application != null && application.Responding)
        {
            return true;
        }

        else
        {
            return false;
        }

    }
    public override void Uninstall(IDictionary savedState)
    {

      base. Uninstall(savedState);
        if (ApplicationIsBusy()==true)
           throw new ApplicationException("Prevent uninstall while application busy.");
    }

  protected override void OnBeforeUninstall(IDictionary savedState)
    {
        base.OnBeforeUninstall(savedState);

        if (ApplicationIsBusy()==true)
            throw new ApplicationException("Prevent uninstall while application busy.");
    }

1 个答案:

答案 0 :(得分:1)

试试这个,

Method to check if application is running

private bool ApplicationIsBusy()
{
   bool isApplicationRunning = false;

   Process[] applicationProcess = Process.GetProcessesByName("applicationname");
   if (null != applicationProcess && applicationProcess.Length > 0)
   {
       isApplicationRunning = true;
   }

   return isApplicationRunning;
}

Uninstall method implementation

public override void Uninstall(IDictionary savedState)
{
  if (ApplicationIsBusy())
      throw new Exception("Prevent uninstall while application busy.");

  base. Uninstall(savedState);
}