我想使用Windows命令提示符(而不是Visual Studio命令提示符)安装Windows服务。
我怎么做呢?
我想使用Windows命令提示符(而不是Visual Studio命令提示符)安装Windows服务。
我怎么做呢?
当前回答
打开Visual studio,在Windows Desktop选项卡中选择Windows Service template,选择新项目。然后将以下代码复制到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." });
}
}
}
右键单击service_name.cs文件,打开服务的设计器。然后右键单击并选择添加安装程序。右键单击serviceProcessInstaller1并将其Account的属性值从User更改为Local System。
从Program.cs文件中删除静态void main方法。 然后保存并构建项目。
注意:goto bin\Ddebug文件夹的项目文件夹。然后打开service_name.exe文件的Properties。然后转到兼容性选项卡。然后单击“更改所有用户的设置”。
选择选项以管理员身份运行此程序。
现在,您必须以管理员身份打开命令提示。 打开后,将目录设置为放置InstallUtil.exe文件的位置。 C:\Windows\Microsoft.NET\Framework64\v4.0.30319 现在写下面的命令:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe -i C:\TimerService\TimerService\bin\Debug\TimerService.exe
注:-i为install he service, -u为uninstall。
在-i设置后,写入您想要安装服务的路径。
现在在commandprompt中按如下方式写入命令:
C:\TimerService\TimerService\bin\Debug>net start service_name
注意:使用stop停止服务。
现在,打开ViewEventLog.exe。选择“Windows日志>应用程序”。在这里,您可以通过启动和停止服务来检查服务的日志。
其他回答
如果目录名有空格,如c:\program files\abc 123,则必须在路径周围使用双引号。
installutil.exe "c:\program files\abc 123\myservice.exe"
如果你像下面这样设置一个bat文件,事情就会简单得多,
例如,要安装一个服务,创建一个“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
要卸载服务,
只需在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"
以管理员身份运行Windows命令提示符 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\到文件夹 installutil C:\ProjectFolder\bin\Debug\MyProject.exe
卸载方法:installutil /u C:\ProjectFolder\bin\Debug\MyProject.exe
请执行以下操作:
以管理员权限启动命令提示符(CMD)。 输入c:\windows\microsoft.net\framework\v4.0.30319\installutil.exe[你的windows服务路径到exe] 按回车键,就是这样!
使用管理员权限打开是很重要的,否则您可能会发现出现一些毫无意义的错误。如果你得到任何,检查你已经打开它与管理权限第一!
要以管理员权限打开,右键单击“命令提示符”并选择“以管理员身份运行”。
来源: http://coderamblings.wordpress.com/2012/07/24/how-to-install-a-windows-service-using-the-command-prompt/
打开开发人员命令提示符为Admin并导航到
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
现在使用你的。exe所在的路径
InstallUtil "D:\backup\WindowsService\WindowsService1\WindowsService1\obj\Debug\TestService.exe"
导航到。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"
关于评论,对于64位应用程序,使用下面的方法:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe