在Windows服务的上下文中删除和卸载有什么区别?

Luc*_*fer 3 .net windows-services uninstall

请帮助我理解使用命令"sc delete"删除Windows服务和使用命令"installutil/u"卸载Windows服务之间的区别.

稍微探讨一下后,我意识到使用installutil命令你必须首先停止windows服务,而sc delete命令负责停止服务本身.如果错误请纠正我.

J0e*_*gan 9

一个相关的SO 问题和答案(特别是两个 - 这里和这里)解释了差异主要是sc delete不需要重新启动和installutil /u确实.关联问题的其他答案也值得检查其他差异.

(小)更新:

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

(大)更新:

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

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

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

  • 卸载时服务是否正在运行
  • 服务是否实现服务(UN)安装程序为System.Configuration.Install.Installer (A CodeProject上的文章,我发现-诚然觉得我多年前读-说明这一点.)

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

// 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.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这些结果与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]
Run Code Online (Sandbox Code Playgroud)

与installutil /us 相同:

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.
Run Code Online (Sandbox Code Playgroud)

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

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