且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在安装程序中添加Windows服务

更新时间:2023-02-22 21:55:28

使用开发人员命令提示符

您可以安装服务在Visual Studio Developer命令提示符中使用以下命令

Using Developer Command Prompt
You can install a service using following command in Visual Studio Developer Command Prompt
installutil.exe YourService.exe



您可以找到Visual工作室开发人员命令提示符

开始 - Visual Studo XXXX - Visual Studio工具 - VSXXXX的开发人员命令提示



确保已使用以管理员身份运行打开此选项(右键单击 - 以管理员身份运行)



使用Windows命令提示符


You can find Visual Studio Developer Command Prompt in
Start -- Visual Studo XXXX -- Visual Studio Tools -- Developer Command Prompt for VSXXXX

Make sure that you have opened this with "Run as administrator" (Right click -- Run as administrator)

Using Windows Command Prompt

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" YourService.exe



注意:您机器上的v4.0.30319可能有所不同



使用安装项目

但是如果你想通过安装项目安装服务那么慢慢地这些步骤 -

1.打开你的服务文件(例如Service1.cs)在设计模式中,右键单击并单击添加安装程序。请注意,现在可以看到2个控件,其名称如 serviceProcessInstaller1 ServiceInstaller1

2.在属性窗格中,更改帐户属性 LocalSystem

3.右键单击 ServiceInstaller1 并将 StartType 属性更改为自动

4.根据您的要求进行更改(如果有)(如服务名称等)

5.添加安装项目到您的解决方案

6.在解决方案资源管理器中,右键单击您的安装项目,选择添加,然后单击项目输出

7.在添加项目输出组对话框的项目框中,单击YourServiceName。单击主要输出,然后单击确定

8.构建整个解决方案。



您的设置应该准备就绪。



更新:




使用批处理文件

1.打开记事本

2。粘贴以下代码并更改服务名称(如果需要,还可以更改框架版本)


Note: v4.0.30319 can be different on your machine

Using Setup Project
But if you want to install the service through setup project then follow these steps-
1. Open your service file (ex. Service1.cs) in Design Mode, right click and click Add Installer. Note that 2 controls will be visible now with name like serviceProcessInstaller1 and ServiceInstaller1
2. n the Properties pane, change the Account property to LocalSystem
3. Right click on ServiceInstaller1 and change StartType property to Automatic
4. Do the changes as per you requirement, if any (like service name etc.)
5. Add a Setup Project to your solution
6. In Solution Explorer, right-click on your setup project, select Add and then click Project Output
7. In the Add Project Output Group dialog box, in the Project box, click YourServiceName. Click Primary Output, and then click OK.
8. Build the whole solution.

Your setup should be ready.

UPDATE:



Using Batch File
1. Open Notepad
2. Paste following code and change the service name (& framework version if required)
@ECHO OFF

REM The following directory is for .NET 4.0 & you may change according to framework version installed in your system
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil" "YourService.exe"



3 。从文件单击 SaveAs 。使用名称和扩展名保存 .bat

4.从放置服务exe的同一位置执行此批处理文件。



希望,它有帮助:)


3. From File click SaveAs. Save it with a name and with extension .bat
4. Execute this batch file from the same location where your service exe is placed.

Hope, it helps :)