【问题标题】:What is the difference between deleting & uninstalling in the context of a Windows Service?在 Windows 服务的上下文中删除和卸载有什么区别?
【发布时间】:2013-04-23 23:39:25
【问题描述】:

请帮助我了解使用命令“sc delete”删除 Windows 服务和使用命令“installutil /u”卸载 Windows 服务之间的区别。

经过一番探索后,我意识到要使用 installutil 命令,您必须首先停止 Windows 服务,而 sc delete 命令负责停止服务本身。如有错误请指正。

【问题讨论】:

    标签: .net windows-services uninstallation


    【解决方案1】:

    一个相关的 SO question 和答案(特别是两个 - herehere)解释了差异,主要是 sc delete 不需要重新启动,而 installutil /u 需要。链接问题的其他答案也值得查看其他差异。

    (小)更新:

    正如您在修改后的问题中指出的那样,是的,链接问题的其他答案突出显示的另一个主要区别是sc delete 将首先停止要删除的服务。

    (大)更新:

    我不满足于简单地信任链接的问题和答案而不进一步验证:事实证明它们并不完全正确 - 至少就我在 Windows 7 Pro x64 上安装和卸载服务的实验而言。

    我看到的是,一个可以在很大程度上体验每个工具所描述的服务卸载; 使用命令卸载并不总是遵循这些模式。

    可能还有更多,但我看到的命令行为的主要决定因素是:

    • 卸载时服务是否正在运行
    • 服务是否以System.Configuration.Install.Installer 的形式实现服务(卸载)安装程序(我发现的CodeProject article - 并且我承认我多年前读过 - 很好地描述了这一点。)

    代替表格来概述我发现的条件和结果,这里有一些伪代码来解释:

    // pseudocode to explain the empirically observed logic of "sc delete" vs.
    // "installutil /u" (on Windows 7 Pro x64)
    //
    // services developed for the investigative experiment:
    //     S[1-4]:    simple Windows services that do *not* implement an
    //                (un)installer
    //     SWI[1-4]:  simple Windows services that implement an (un)installer
    if serviceIsRunning
    {
        if "sc delete"
        {
            // Using:   S1, SWI1
            // Result:  The service is not stopped but disabled and marked for deletion, requiring a reboot.
        }
        else // "installutil /u"
        {
            if serviceImplementsInstaller
            {
                // Using:   SWI2
                // Result:  The service is stopped, disabled, and marked for deletion, requiring a reboot.
            }
            else
            {
                // Using:   S2
                // Result:  The service is not stopped, disabled, or marked for deletion - still installed post-reboot.
            }
        }
    }
    else // serviceIsRunning == false
    {
        if "sc delete"
        {
            // Using:   S3, SWI3
            // Result:  The service is deleted - no reboot required.
        }
        else // "installutil /u"
        {
            if serviceImplementsInstaller
            {
                // Using:   SWI4
                // Result:  The service is deleted - no reboot required.
            }
            else
            {
                // Using:   S4
                // Result:  The service is not disabled or marked for deletion - still installed post-reboot.
            }
        }
    }
    

    这些结果与sc delete自己的文档一致:

    C:\Windows\system32>sc delete
    DESCRIPTION:
            Deletes a service entry from the registry.
            If the service is running, or another process has an
            open handle to the service, the service is simply marked
            for deletion.
    USAGE:
            sc <server> delete [service name]
    

    installutil /u's 相同:

    C:\Windows\system32>installutil /u
    Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    Usage: InstallUtil [/u | /uninstall] [option [...]] assembly [[option [...]] assembly] [...]]
    
    InstallUtil executes the installers in each given assembly.
    If the /u or /uninstall switch is specified, it uninstalls
    the assemblies, otherwise it installs them.
    

    请注意,当您在未实现(卸载)安装程序的服务上使用 installutil /u 时,其输出中会显示以下消息:

    找不到具有 RunInstallerAttribute.Yes 属性的公共安装程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 2013-03-11
      • 2011-08-18
      相关资源
      最近更新 更多