首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UninstallString执行C#

使用UninstallString执行C#
EN

Stack Overflow用户
提问于 2011-09-04 06:55:09
回答 3查看 3.4K关注 0票数 0

我在使用进程执行uninstallString时遇到了问题,它不能在所有情况下都工作。我需要一个通用的过程,在任何情况下都会运行。

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

代码:

代码语言:javascript
复制
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();
  • 我的第二个想法是:

代码:

代码语言:javascript
复制
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);
        }
    }
}

但它还是不能涵盖所有的案件。

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

EN

回答 3

Stack Overflow用户

发布于 2011-09-04 07:06:36

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

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

代码语言:javascript
复制
HKLM\Software\Microsoft\Windows\Currentversion\Uninstall\{NameOfApplication}\UninstallString

上面标记为{NameOfApplication}的部分应该为可以卸载的所有程序都有一个条目。有关详细信息,请参阅卸载注册表键

票数 0
EN

Stack Overflow用户

发布于 2011-09-05 10:26:58

代码语言:javascript
复制
//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;
                       }
票数 0
EN

Stack Overflow用户

发布于 2015-02-28 09:59:02

下面是我的代码,使用与Roy相同的方式,也许简单一点:

代码语言:javascript
复制
  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;
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7297814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档