我想使用Windows命令提示符(而不是Visual Studio命令提示符)安装Windows服务。

我怎么做呢?


当前回答

安装服务:

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

卸载服务:-

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "C:\Services\myservice.Service.exe"

其他回答

以下代码,安装和卸载服务,

打开命令提示符,以管理员身份运行程序,输入以下命令并按enter。

语法

安装

C:\windows\microsoft.net\framework\v4.0.30319>InstallUtil.exe + Your copied path + \your service name + .exe

我们的道路 InstallUtil.exe C: \ MyFirstService \ bin \ \ MyFirstService.exe调试

要卸载

 C:\windows\microsoft.net\framework\v4.0.30319>InstallUtil.exe -u + Your copied path + \your service name + .exe

例如:我们的路径InstallUtil.exe -u C:\MyFirstService\bin\Debug\MyFirstService.exe

要获得更多帮助,您可以查看以下链接:示例程序

如果你正在使用Powershell并且你想要安装。net服务,你可以使用install - service模块。它是InstalUtil工具的包装器。

它公开了3个命令

Install-Service -调用InstallUtil.exe pathToExecutable命令 Install-ServiceIfNotInstalled -首先检查服务是否存在 如果不执行Install-Service方法,则安装 Uninstall-Service-它卸载服务。可执行文件路径的ServiceName可以使用。

这个模块的代码可以在这里查看

打开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日志>应用程序”。在这里,您可以通过启动和停止服务来检查服务的日志。

导航到。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

当您的程序集版本和您的Visual studio项目在dot net 2或4上的biild设置安装相同的版本时。

使用installutil安装相同版本的service

如内置点网4个

输入c: \ windows \ microsoft.net \ framework \ v4.0.30319 \ installutil.exe

如内置点网2

输入c: \ windows \ microsoft.net \ framework \ v2.0.11319 \ installutil.exe