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

我怎么做呢?


当前回答

SC Create命令没有问题。 你只需要知道正确的参数:

SC CREATE "MySVC" binpath= "D:\Me\Services\MySVC\MySVC.exe"

其他回答

打开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

您可以使用InstallUtil安装任何windows服务。 1: C:\Windows\Microsoft.NET\Framework64\v4.0.30319在命令提示符下以管理员身份运行。 2:复制Exe路径并输入InstallUtil.exe "your Exe path" 然后按回车键。

如果你想要视觉化。转到下面的链接。 这对我帮助很大。

https://youtu.be/yrdyYxzI7SE

安装服务:

"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"

1.从开始菜单中,选择Visual Studio目录,然后为VS选择开发人员命令提示符。

2.出现Visual Studio的开发人员命令提示符。

3.访问项目已编译的可执行文件所在的目录。

4.以项目的可执行文件作为参数,从命令提示符运行InstallUtil.exe