[英]Install a Windows service using a Windows command prompt?
I want to install a Windows service using a Windows command prompt (not the Visual Studio command prompt).我想使用 Windows 命令提示符(而不是 Visual Studio 命令提示符)安装 Windows 服务。
How do I do this?我该怎么做呢?
导航到 .net 文件夹中的 installutil.exe(例如,对于 .net 4,它是 C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319)并使用它来安装您的服务,如下所示:
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" "c:\myservice.exe"
Nothing wrong with SC Create command. SC Create 命令没有问题。 Just you need to know the correct args :
只是你需要知道正确的 args :
SC CREATE "MySVC" binpath= "D:\Me\Services\MySVC\MySVC.exe"
If the directory's name has a space like c:\\program files\\abc 123
, then you must use double quotes around the path.如果目录的名称有像
c:\\program files\\abc 123
这样的空格,那么您必须在路径周围使用双引号。
installutil.exe "c:\program files\abc 123\myservice.exe"
It makes things much easier if you set up a bat file like following,如果你像下面这样设置一个 bat 文件,它会让事情变得更容易,
eg To install a service, create a "myserviceinstaller.bat" and " Run as Administrator "例如,要安装服务,请创建“myserviceinstaller.bat”和“以管理员身份运行”
@echo off
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
installutil.exe "C:\Services\myservice.exe"
if ERRORLEVEL 1 goto error
exit
:error
echo There was a problem
pause
to uninstall service,卸载服务,
Just add a -u to the installutil command.只需在 installutil 命令中添加-u即可。
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "C:\Services\myservice.exe"
Perform the following:执行以下操作:
c:\\windows\\microsoft.net\\framework\\v4.0.30319\\installutil.exe [your windows service path to exe]
c:\\windows\\microsoft.net\\framework\\v4.0.30319\\installutil.exe [your windows service path to exe]
It's important to open with administrator rights otherwise you may find errors that come up that don't make sense.以管理员权限打开很重要,否则您可能会发现出现的错误。 If you get any, check you've opened it with admin rights first!
如果您收到任何文件,请先检查您是否已使用管理员权限打开它!
To open with admin rights , right click 'Command Prompt' and select 'Run as administrator'.要以管理员权限打开,请右键单击“命令提示符”并选择“以管理员身份运行”。
Source: http://coderamblings.wordpress.com/2012/07/24/how-to-install-a-windows-service-using-the-command-prompt/来源: http : //coderamblings.wordpress.com/2012/07/24/how-to-install-a-windows-service-using-the-command-prompt/
Install Service:-安装服务:-
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe"
"C:\Services\myservice.exe"
UnInstall Sevice:-卸载服务:-
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "C:\Services\myservice.Service.exe"
I must add one more point in this thread.我必须在此线程中再添加一点。 To install/uninstall
64-bit
version of assemblies one should use 64-bit
version of tool.要安装/卸载
64-bit
版本的程序集,应使用64-bit
版本的工具。 To install a service, the command should be:要安装服务,命令应该是:
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe"
"C:\YourFolder\YourService.exe"
and to uninstall the command should be:并卸载命令应该是:
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" -u
"C:\YourFolder\YourService.exe"
Create a *.bat
file beside of your windows service exe
file for installing with the following context:在 Windows 服务
exe
文件旁边创建一个*.bat
文件,以便使用以下上下文进行安装:
CLS
ECHO Installing My Windows Service
START %windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe "%~d0%~p0\YourWindowsServiceExeName.exe"
Create a *.bat
file beside of your windows service exe
file for uninstalling with the following context:在 Windows 服务
exe
文件旁边创建一个*.bat
文件,以便使用以下上下文进行卸载:
CLS
ECHO Uninstalling My Windows Service
START %windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "%~d0%~p0\YourWindowsServiceExeName.exe"
Run each of bat
file as Admin to install or uninstall your windows service.以管理员身份运行每个
bat
文件以安装或卸载您的 Windows 服务。
cd C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\
to go to foldercd C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\
转到文件夹installutil C:\\ProjectFolder\\bin\\Debug\\MyProject.exe
installutil C:\\ProjectFolder\\bin\\Debug\\MyProject.exe
Note: To uninstall: installutil /u C:\\ProjectFolder\\bin\\Debug\\MyProject.exe
注意:要卸载:
installutil /u C:\\ProjectFolder\\bin\\Debug\\MyProject.exe
If you are using Powershell and you want to install .NET service you can use Install-Service module.如果您正在使用 Powershell 并且想要安装 .NET 服务,您可以使用Install-Service模块。 It is a wrapper for InstalUtil tool.
它是 InstalUtil 工具的包装器。
It exposes 3 commands它公开了 3 个命令
Open Visual studio and select new project by selecting Windows Service
template in Windows Desktop
tab.打开 Visual Studio 并通过在
Windows Desktop
选项卡中选择Windows Service
模板来选择新项目。 Than copy following code into your service_name.cs file.然后将以下代码复制到您的 service_name.cs 文件中。
using System.Diagnostics;
using System.ServiceProcess;
namespace TimerService
{
public partial class Timer_Service : ServiceBase
{
public Timer_Service()
{
InitializeComponent();
}
static void Main()
{
if (System.Diagnostics.Debugger.IsAttached)
{
Timer_Service service = new Timer_Service();
service.OnStart(null);
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Timer_Service()
};
ServiceBase.Run(ServicesToRun);
}
}
protected override void OnStart(string[] args)
{
EventLog.WriteEvent("Timer_Service", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Service start successfully." });
}
protected override void OnStop()
{
EventLog.WriteEvent("Timer_Service", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Service stop successfully." });
}
}
}
Right-Click on service_name.cs file and open designer of service.右键单击 service_name.cs 文件并打开服务设计器。 than right-click and select
Add Installer
.比右键单击并选择
Add Installer
。 than right-click on serviceProcessInstaller1
and change its property value of Account
from User
to Local System
.而不是右键单击
serviceProcessInstaller1
并将其Account
属性值从User
更改为Local System
。
Remove static void main
method from Program.cs
file.从
Program.cs
文件中删除static void main
方法。 Than save and Build your project.比保存和构建您的项目。
NOTE: goto bin\\Ddebug
folder of your project folder.注意:转到项目文件夹的
bin\\Ddebug
文件夹。 Than open Properties of your service_name.exe
file.比打开
service_name.exe
文件的属性。 Than goto Compatibility
tab.比转到
Compatibility
选项卡。 Than click on Change Settings For All Users
.然后单击
Change Settings For All Users
。
Select option Run this program as an administrator
.选择选项
Run this program as an administrator
。
Now, You have to open CommandPromt as Administrator.现在,您必须以管理员身份打开 CommandPromt。 After open, set directory to where your
InstallUtil.exe
file is placed.打开后,将目录设置为您的
InstallUtil.exe
文件所在的位置。 for ex: C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319
.例如:
C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319
。 now write the following command:现在编写以下命令:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe -i C:\TimerService\TimerService\bin\Debug\TimerService.exe
Note: -i is for install he service and -u for Unsinstall.注意: -i 用于安装服务,-u 用于卸载。
after -i set the write the path where you want to install your service.在 -i 设置写入要安装服务的路径之后。
now write the command in CommandPromt as follows:现在在 CommandPromt 中编写命令如下:
C:\TimerService\TimerService\bin\Debug>net start service_name
Note: use stop
for stop the Service.注意:使用
stop
来停止服务。
Now, open ViewEventLog.exe
.现在,打开
ViewEventLog.exe
。 Select Windows Logs>Application.选择 Windows 日志 > 应用程序。 There you can check your Service's log by start and stop the service.
在那里您可以通过启动和停止服务来检查您的服务日志。
the following code , install and uninstall the Service,以下代码,安装和卸载服务,
Open the command prompt and run the program as an administrator and fire the below command and press enter.打开命令提示符并以管理员身份运行程序,然后执行以下命令并按 Enter。
Syntax句法
To Install安装
C:\windows\microsoft.net\framework\v4.0.30319>InstallUtil.exe + Your copied path + \your service name + .exe
eg :Our Path InstallUtil.exe C:\\MyFirstService\\bin\\Debug\\MyFirstService.exe例如:我们的路径 InstallUtil.exe C:\\MyFirstService\\bin\\Debug\\MyFirstService.exe
To uninstall卸载
C:\windows\microsoft.net\framework\v4.0.30319>InstallUtil.exe -u + Your copied path + \your service name + .exe
eg : Our path InstallUtil.exe -u C:\\MyFirstService\\bin\\Debug\\MyFirstService.exe例如:我们的路径 InstallUtil.exe -u C:\\MyFirstService\\bin\\Debug\\MyFirstService.exe
for more help you can see the following link: sample program如需更多帮助,您可以查看以下链接:示例程序
open Developer command prompt as Admin and navigate to以管理员身份打开开发人员命令提示符并导航到
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
Now use path where is your .exe
there现在使用你的
.exe
所在的路径
InstallUtil "D:\backup\WindowsService\WindowsService1\WindowsService1\obj\Debug\TestService.exe"
when your assembly version and your Visual studio project Biuld setting on dot net 2 or 4 install with same version.当您的程序集版本和 dot net 2 或 4 上的 Visual Studio 项目 Biuld 设置安装相同版本时。
install service with installutil
that same version使用
installutil
安装相同版本的服务
if build in dot net 4如果在 dot net 4 中构建
Type c:\\windows\\microsoft.net\\framework\\v4.0.30319\\installutil.exe
键入
c:\\windows\\microsoft.net\\framework\\v4.0.30319\\installutil.exe
if build in dot net 2如果在 dot net 2 中构建
Type c:\\windows\\microsoft.net\\framework\\v2.0.11319\\installutil.exe
键入
c:\\windows\\microsoft.net\\framework\\v2.0.11319\\installutil.exe
Follow these steps when deploying the Windows Service, don't lose time:部署 Windows 服务时,请按照以下步骤操作,不要浪费时间:
Run command prompt by the Admin right通过管理员权限运行命令提示符
Insure about release mode when compilling in your IDE在 IDE 中编译时确保发布模式
Give a type to your project installer on design view在设计视图中为您的项目安装程序指定一个类型
Insure about software dependencies: If you are using a certificate install it correctly确保软件依赖性:如果您使用的是证书,请正确安装
Go your console write this:去你的控制台写这个:
C:\\Windows\\Microsoft.NET\\Framework\\yourRecentVersion\\installutil.exe c:\\yourservice.exe C:\\Windows\\Microsoft.NET\\Framework\\yourRecentVersion\\installutil.exe c:\\yourservice.exe
there is a hidden -i argument before the exe path -ic:\\
you can use -u
for uninstallling在 exe 路径
-ic:\\
之前有一个隐藏的 -i 参数,您可以使用-u
进行卸载
1.From the Start menu, select the Visual Studio directory, then select Developer Command Prompt for VS . 1.从开始菜单中,选择Visual Studio目录,然后选择VS的Developer Command Prompt。
2.The Developer Command Prompt for Visual Studio appears. 2.Visual Studio 的开发人员命令提示符出现。
3.Access the directory where your project's compiled executable file is located. 3.访问你的项目编译后的可执行文件所在的目录。
4.Run InstallUtil.exe from the command prompt with your project's executable as a parameter 4.从命令提示符运行 InstallUtil.exe,并将项目的可执行文件作为参数
You can use InstallUtil to install any windows service.您可以使用 InstallUtil 安装任何 Windows 服务。 1: C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319 in command prompt running as Adminstrator.
1:C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319 在以管理员身份运行的命令提示符下。 2: Copy the Exe path and type InstallUtil.exe "your exe path" and hit enter.
2:复制exe路径并输入InstallUtil.exe“你的exe路径”并回车。
If you want visual elaboration.如果你想要视觉阐述。 Goto below link.
转到下面的链接。 It helped me alot.
这对我帮助很大。
If you face error installing the service then do the following:如果您在安装服务时遇到错误,请执行以下操作:
Simply right click on your service files and select properties and then check the unblock checkbox只需右键单击您的服务文件并选择属性,然后选中取消阻止复选框
https://brianseekford.azurewebsites.net/2011/07/13/hresult-0x80131515-when-running-installutil-to-install-a-net-service/ https://brianseekford.azurewebsites.net/2011/07/13/hresult-0x80131515-when-running-installutil-to-install-a-net-service/
您可以使用命令提示符并写入:C:\\windows\\microsoft.net\\framework\\v4.0.30319\\InstallUtil.exe -i ".EXE file of window service"
您应该打开命令提示符,转到
C:\\windows\\microsoft.net\\framework\\v4.0.30319\\InstallUtil.exe -i ".EXE file of window service"
Open command prompt as administrator, go to your Folder where your .exe
resides.以管理员身份打开命令提示符,转到
.exe
所在的文件夹。 To Install Exe
as service将
Exe
安装为服务
D:\YourFolderName\YourExeName /i
To uninstall use /u
.要卸载使用
/u
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.