【问题标题】:Executing UninstallString using C#使用 C# 执行卸载字符串
【发布时间】:2011-11-10 00:00:59
【问题描述】:

我在使用进程执行 uninstallString 时遇到问题,它并非在所有情况下都有效。 我需要一个在任何情况下都能运行的通用程序。

  • 我的一个想法是解析卸载字符串

代码:

int indexOfExe = uninstallString.ToLower().IndexOf(".exe") + 4;
string exeFile = uninstallString.Substring(0, indexOfExe).Trim();
string args = uninstallString.Substring(indexOfExe, uninstallString.Length - indexOfExe).Trim();

if (args.Length > 0)
{
    procStartInfo.FileName = exeFile;
    procStartInfo.Arguments = args;
}
else
{
    procStartInfo.FileName = exeFile;
    procStartInfo.Arguments = "";
}

procStartInfo.Verb = "runas";
procStartInfo.CreateNoWindow = true;
procStartInfo.UseShellExecute = false ;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
  • 我的第二个想法是:

代码:

if (uninstallString.Contains("msiexec"))
{
    uninstallString = uninstallString.Replace("\"", "");
    uninstallString = RegistryHandler.getCommandInCommaAndArgumentsOutside(uninstallString);
}
else
{
    procStartInfo.FileName = "cmd";

    string[] words = uninstallString.Split("/".ToCharArray());

    if (uninstallString.StartsWith(@"""") && words.Count() == 1)
    {
        procStartInfo.FileName = uninstallString;
        procStartInfo.Arguments = "";
    }
    else
    {
        //procStartInfo.Arguments = "/c " + "\"" + uninstallString + "\"";
        if ((uninstallString.StartsWith(@"""") && words.Count() > 1))
        {
            procStartInfo.Arguments = "/c " + uninstallString;
        }
        else
        {
            procStartInfo.Arguments = "/c " + RegistryHandler.getCommandInCommaAndArgumentsOutsideByExe(uninstallString);
        }
    }
}

但它仍然不会涵盖所有情况。

所有情况的通用解决方案是什么?

【问题讨论】:

  • 您已经描述了您尝试过的内容,但没有描述出了什么问题。您是否尝试替换添加/删除程序对话框?
  • 在命令行进程中直接执行uninstallstring键中的值有什么问题?卸载是否必须在没有用户干预的情况下进行?

标签: c# registry uninstallation uninstallstring


【解决方案1】:

这是我的代码,使用和 Roy 一样的方法,也许简单一点:

  private string SwitchCondition(string uninstallstring)
    {
        if (uninstallstring.Substring(0, 1).Equals("\"") |
            uninstallstring.ToLower().Contains("msiexec") |
            uninstallstring.Contains("~"))
        {
            Debug.WriteLine(uninstallstring);
        }
        else if (uninstallstring.ToLower().IndexOf(".exe") > 0)
        {
            uninstallstring = "\"" + uninstallstring.Insert(uninstallstring.ToLower().IndexOf(".exe") + 4, "\"");
            Debug.WriteLine("Contains .exe" + uninstallstring);
        }
        else
        {
            uninstallstring = "\"" + uninstallstring + "\"";
            Debug.WriteLine("Case end " + uninstallstring);
        }

        return uninstallstring;
    }

【讨论】:

    【解决方案2】:
    //i wrote this code, which is working in most of the cases :
    
    //----------------------------------------------------------------------------------------------          
    
                if (uninstallString.Contains("msiexec"))
                    {
                    uninstallString = uninstallString.Replace("\"", "");
                    uninstallString = RegistryHandler.getCommandInCommaAndArgumentsOutside(uninstallString);
                    }
                    else
                    {
                      if (uninstallString.StartsWith(@""""))
                         {
                         int indexOfLastComma = uninstallString.IndexOf("\"", 1) + 1;
                         procStartInfo.FileName = uninstallString.Substring(0, indexOfLastComma);
                         procStartInfo.Arguments = uninstallString.Substrin(indexOfLastComma,uninstallString.Length - indexOfLastComma));
    
                         }
                         else
                         {
                          procStartInfo.FileName = "cmd.exe";
                          procStartInfo.Arguments = "/c " + RegistryHandler.getCommandInCommaAndArgumentsOutsideByExe(uninstallString); 
                          }     
    }
    
    //----------------------------------------------------------------------------------------------
    
    
              public static string getCommandInCommaAndArgumentsOutsideByExe(string command)
                     {
                       int ind = 0;
                       string[] prms = command.Split(' ');
    
                       ind = prms[0].Length; //command.IndexOf(".exe") + 4;
    
                       int exeLocationIndex = command.IndexOf(".exe") + 4;
                       string cmd = command.Substring(0, exeLocationIndex);
                       string arguments = command.Substring(command.IndexOf(".exe") + 4, command.Length - exeLocationIndex);
    
                       return "\"" + cmd + "\"" + arguments;
                           }
    

    【讨论】:

      【解决方案3】:

      从技术上讲,您的第二个想法应该可行(适用于所有使用 Windows Installer 的程序)。但是,您需要获取正确的卸载字符串。我怀疑问题是您的卸载字符串不正确。

      您应该能够通过查看以下内容来查询注册表中的卸载字符串:

      HKLM\Software\Microsoft\Windows\Currentversion\Uninstall\{NameOfApplication}\UninstallString
      

      上面标记为{NameOfApplication}的部分应该包含所有可卸载程序的条目。详情请见Uninstall Registry Key

      【讨论】:

      • 是的,我知道那是uninstallString 注册表路径。但我的第二个想法仍然不能涵盖所有情况。微软正在做什么以卸载软件(MSI 和非 MSI)?
      • @Roy:微软没有 - 我的软件没有显示在控制面板中 - 但是,其他软件应该放入卸载字符串。这就是控制面板用来查找要卸载的软件的全部内容。例如,“CutePDF”不使用 MSI,所以我系统上的卸载字符串是“C:\Program Files (x86)\Acro Software\CutePDF Writer\Setup64.exe /uninstall”
      猜你喜欢
      • 2013-11-02
      • 2011-02-25
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      相关资源
      最近更新 更多