microsoft 如何:安装和卸载 Windows 服务
如何:安装和卸载 Windows 服务
<nav id="center-doc-outline" class="doc-outline is-hidden-desktop" data-bi-name="intopic toc" role="navigation" aria-label="文章大纲">
<h3>本文内容</h3>
<ol><li><a href="#install-using-installutilexe-utility">使用 InstallUtil.exe 实用程序安装</a></li><li><a href="#uninstall-using-installutilexe-utility">使用 InstallUtil.exe 实用程序卸载</a></li><li><a href="#install-using-powershell">使用 PowerShell 安装</a></li><li><a href="#uninstall-using-powershell">使用 PowerShell 卸载</a></li><li><a href="#see-also">请参阅</a></li></ol></nav>
<p><span data-ttu-id="968d6-105">如果正使用 .NET Framework 开发 Windows 服务,可以使用 <a href="../tools/installutil-exe-installer-tool" data-linktype="relative-path">InstallUtil.exe</a> 命令行实用工具或 <a href="/zh-cn/powershell/scripting/overview" data-linktype="absolute-path">PowerShell</a> 快速安装服务应用。</span><span class="sxs-lookup"><span data-stu-id="968d6-105">If you\'re developing a Windows service with the .NET Framework, you can quickly install your service app by using the <a href="../tools/installutil-exe-installer-tool" data-linktype="relative-path"><em>InstallUtil.exe</em></a> command-line utility or <a href="/zh-cn/powershell/scripting/overview" data-linktype="absolute-path">PowerShell</a>.</span></span> <span data-ttu-id="968d6-106">如果开发人员希望发布用户可安装和卸载的 Windows 服务,则可以使用免费的 <a href="https://wixtoolset.org/" data-linktype="external">WiX 工具集</a>或<a href="https://www.advancedinstaller.com/" data-linktype="external">高级安装程序</a>、<a href="https://www.revenera.com/install/products/installshield.html" data-linktype="external">InstallShield</a> 等其他商业工具。</span><span class="sxs-lookup"><span data-stu-id="968d6-106">Developers who want to release a Windows service that users can install and uninstall can use the free <a href="https://wixtoolset.org/" data-linktype="external">WiX Toolset</a> or commercial tools like <a href="https://www.advancedinstaller.com/" data-linktype="external">Advanced Installer</a>, <a href="https://www.revenera.com/install/products/installshield.html" data-linktype="external">InstallShield</a>, or others.</span></span> <span data-ttu-id="968d6-107">有关详细信息,请参阅<a href="/zh-cn/visualstudio/deployment/deploying-applications-services-and-components#create-an-installer-package-windows-desktop" data-linktype="absolute-path">创建安装程序包(Windows 桌面)</a>。</span><span class="sxs-lookup"><span data-stu-id="968d6-107">For more information, see <a href="/zh-cn/visualstudio/deployment/deploying-applications-services-and-components#create-an-installer-package-windows-desktop" data-linktype="absolute-path">Create an installer package (Windows desktop)</a>.</span></span></p>
警告
如果要从计算机中卸载服务,请勿按照本文中的步骤进行操作。If you want to uninstall a service from your computer, don\’t follow the steps in this article. 而是找出安装了该服务的程序或软件包,然后在“设置”中选择“应用”来卸载该程序。Instead, find out which program or software package installed the service, and then choose Apps in Settings to uninstall that program. 请注意,许多服务是 Windows 不可或缺的部分;如果你删除它们,可能导致系统不稳定。Note that many services are integral parts of Windows; if you remove them, you might cause system instability.
要使用本文中的步骤,首先需要将服务安装程序添加到 Windows 服务。To use the steps in this article, you first need to add a service installer to your Windows service. 有关详细信息,请参见演练:创建 Windows 服务应用。For more information, see Walkthrough: Creating a Windows service app.
无法通过按 F5 从 Visual Studio 开发环境直接运行 Windows 服务项目。You can\’t run Windows service projects directly from the Visual Studio development environment by pressing F5. 必须先在项目中安装服务,然后才能运行该项目。Before you can run the project, you must install the service in the project.
提示
可以使用“服务器资源管理器”验证是否已安装或卸载服务。You can use Server Explorer to verify that you\’ve installed or uninstalled your service.
使用 InstallUtil.exe 实用程序安装Install using InstallUtil.exe utility
-
从“开始”菜单中选择“Visual Studio <version>”目录,然后选择“VS <version> 开发人员命令提示” 。From the Start menu, select the Visual Studio <version> directory, then select Developer Command Prompt for VS <version>.
出现“Visual Studio 开发人员命令提示”。The Developer Command Prompt for Visual Studio appears.
-
访问你的项目的已编译可执行文件所在的目录。Access the directory where your project\’s compiled executable file is located.
-
将项目的可执行文件作为参数,通过命令提示运行 InstallUtil.exe:Run InstallUtil.exe from the command prompt with your project\’s executable as a parameter:
控制台installutil <yourproject>.exe
如果使用的是 Visual Studio 开发人员命令提示,InstallUtil.exe 应该在系统路径上。If you’re using the Developer Command Prompt for Visual Studio, InstallUtil.exe should be on the system path. 如果不在,可以将其添加到该路径,或使用完全限定的路径来调用它。Otherwise, you can add it to the path, or use the fully qualified path to invoke it. 此工具随 .NET Framework 安装在 %WINDIR%\Microsoft.NET\Framework[64]\<framework_version> 中。This tool is installed with the .NET Framework in %WINDIR%\Microsoft.NET\Framework[64]\<framework_version>.
例如:For example:
- 对于 32 位版的 .NET Framework 4 或 4.5 以及更高版本,如果 Windows 安装目录是 C: \ Windows,则默认路径是 C:\Windows\ Microsoft.NET \ Framework \ v4.0.30319 \ InstallUtil.exe 。For the 32-bit version of the .NET Framework 4 or 4.5 and later, if your Windows installation directory is C:\Windows, the default path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe.
- 对于 64 位版的 .NET Framework 4 或 4.5 以及更高版本,默认路径是 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe。For the 64-bit version of the .NET Framework 4 or 4.5 and later, the default path is C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe.
使用 InstallUtil.exe 实用程序卸载Uninstall using InstallUtil.exe utility
-
从“开始”菜单中选择“Visual Studio <version>”目录,然后选择“VS <version> 开发人员命令提示” 。From the Start menu, select the Visual Studio <version> directory, then select Developer Command Prompt for VS <version>.
出现“Visual Studio 开发人员命令提示”。The Developer Command Prompt for Visual Studio appears.
-
将项目的输出作为参数,通过命令提示运行 InstallUtil.exe:Run InstallUtil.exe from the command prompt with your project\’s output as a parameter:
控制台installutil /u <yourproject>.exe
-
删除服务的可执行文件后,该服务可能仍然会出现在注册表中。After the executable for a service is deleted, the service might still be present in the registry. 如果发生这种情况下,请使用命令 sc delete 从注册表中删除服务的条目。If that\’s the case, use the command sc delete to remove the entry for the service from the registry.
使用 PowerShell 安装Install using PowerShell
-
从“开始”菜单中,选择“Windows PowerShell”目录,然后选择“Windows PowerShell”。From the Start menu, select the Windows PowerShell directory, then select Windows PowerShell.
-
访问你的项目的已编译可执行文件所在的目录。Access the directory where your project\’s compiled executable file is located.
-
运行 New-Service cmdlet,并将项目的输出和服务名称作为参数:Run the New-Service cmdlet with the with your project\’s output and a service name as parameters:
PowerShellNew-Service -Name "YourServiceName" -BinaryPathName <yourproject>.exe
使用 PowerShell 卸载Uninstall using PowerShell
-
从“开始”菜单中,选择“Windows PowerShell”目录,然后选择“Windows PowerShell”。From the Start menu, select the Windows PowerShell directory, then select Windows PowerShell.
-
运行 Remove-Service cmdlet,并将服务名称作为参数:Run the Remove-Service cmdlet with the name of your service as parameter:
PowerShellRemove-Service -Name "YourServiceName"
-
删除服务的可执行文件后,该服务可能仍然会出现在注册表中。After the executable for a service is deleted, the service might still be present in the registry. 如果发生这种情况下,请使用命令 sc delete 从注册表中删除服务的条目。If that\’s the case, use the command sc delete to remove the entry for the service from the registry.
PowerShellsc.exe delete "YourServiceName"
请参阅See also
- Windows 服务应用程序简介Introduction to Windows service applications
- 如何:创建 Windows 服务How to: Create Windows services
- 如何:将安装程序添加到服务应用程序How to: Add installers to your service application
- Installutil.exe(安装程序工具)Installutil.exe (Installer tool)
<!-- </content> -->
</main>