首页 文章

使用Windows命令提示符安装Windows服务?

提问于
浏览
233

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

我该怎么做呢?

14 回答

  • 0
    • 以管理员权限启动命令提示符(CMD) .

    • 键入c:\ windows \ microsoft.net \ framework \ v4.0.30319 \ installutil.exe [你的windows服务路径到exe]

    • 按返回

  • 67

    导航到.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"
    
  • 2

    如果目录的名称有 c:\program files\abc 123 之类的空格,则必须在路径周围使用双引号 .

    installutil.exe "c:\program files\abc 123\myservice.exe"
    

    Install windows service from command prompt

    如果您设置如下的bat文件,它会使事情变得更容易,

    例如要安装服务,请创建"myserviceinstaller.bat"和“ Run as Administrator

    @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
    

    卸载服务,

    只需将 -u 添加到installutil命令即可 .

    cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
    
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "C:\Services\myservice.exe"
    
  • 2

    SC Create命令没有错 . 只是你需要知道正确的args:

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

    执行以下操作:

    • 使用管理员权限启动命令提示符(CMD) .

    • 类型 c:\windows\microsoft.net\framework\v4.0.30319\installutil.exe [your windows service path to exe]

    • 按回车就是那个!

    使用管理员权限打开很重要,否则您可能会发现无意义的错误 . 如果你得到任何,请检查你是否先用管理员权限打开它!

    To open with admin rights ,右键单击'Command Prompt'并选择'Run as administrator' .

    资料来源:http://coderamblings.wordpress.com/2012/07/24/how-to-install-a-windows-service-using-the-command-prompt/

  • 72

    我必须在这个帖子中再加一点 . 要安装/卸载64位版本的程序集,应使用64位版本的工具 . 要安装服务,该命令应为:

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

    并卸载命令应该是:

    "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" -u
    "C:\YourFolder\YourService.exe"
    
  • -1

    Install Service:-

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

    UnInsatall Sevice:-

    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "C:\Services\myservice.Service.exe"
    
  • 404
    • 运行Windows命令提示符为 Administrator

    • 粘贴此代码: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\ 转到文件夹

    • 也可以编辑并运行它: installutil C:\ProjectFolder\bin\Debug\MyProject.exe

    Note: 要卸载: installutil /u C:\ProjectFolder\bin\Debug\MyProject.exe

  • 4

    如果您使用Powershell并且想要安装.NET服务,则可以使用Install-Service模块 . 它是InstalUtil工具的包装器 .

    它公开了3个命令

    • Install-Service - 调用InstallUtil.exe pathToExecutable命令

    • Install-ServiceIfNotInstalled - 如果不执行Install-Service方法,首先检查是否安装了服务

    • 卸载 - 服务 - 它卸载服务 . 可以使用可执行路径的ServiceName .

    可以查看此模块的代码here

  • 1

    使用以下上下文在Windows服务 exe file for installing 旁边创建一个 *.bat 文件:

    CLS
    ECHO Installing My Windows Service 
    
    START %windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe "%~d0%~p0\YourWindowsServiceExeName.exe"
    

    使用以下上下文在Windows服务 exe file for uninstalling 旁边创建一个 *.bat 文件:

    CLS
    ECHO Uninstalling My Windows Service 
    
    START %windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "%~d0%~p0\YourWindowsServiceExeName.exe"
    

    运行 bat file as Admin 中的每一个以安装或卸载Windows服务 .

  • 1

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

    打开 command prompt and run the program as an administrator 并触发以下命令,然后按Enter键 .

    Syntax

    To Install

    C:\windows\microsoft.net\framework\v4.0.30319>InstallUtil.exe + Your copied path + \your service name + .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
    

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

    如需更多帮助,您可以看到以下链接:sample program

  • 4

    通过在 Windows Desktop 选项卡中选择 Windows Service 模板打开Visual Studio并选择新项目 . 将以下代码复制到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文件并打开服务设计器 . 然后右键单击并选择 Add Installer . 然后右键单击 serviceProcessInstaller1 并将 Account 的属性值从 User 更改为 Local System .

    Program.cs 文件中删除 static void main 方法 . 比保存和构建您的项目 .

    NOTE: goto bin\Ddebug 项目文件夹的文件夹 . 比打开 service_name.exe 文件的属性 . 比转到 Compatibility 标签 . 然后单击 Change Settings For All Users .

    选择选项 Run this program as an administrator .

    现在,您必须以管理员身份打开CommandPromt . 打开后,将目录设置为 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
    

    Note: -i用于安装服务,-u用于卸载 .

    在-i之后设置写入您要安装服务的路径 .

    现在在CommandPromt中编写命令如下:

    C:\TimerService\TimerService\bin\Debug>net start service_name
    

    Note: 使用 stop 来停止服务 .

    现在,打开 ViewEventLog.exe . 选择Windows日志>应用程序 . 在那里,您可以通过开始和停止检查您的服务日志服务 .

  • 2

    当你的汇编版本和你的Visual Studio项目在点网2或4上的Biuld设置安装相同的版本 .

    使用 installutil 安装相同版本的服务

    if build in dot net 4

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

    if build in dot net 2

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

  • 0

    你应该打开命令提示符,转到

    C:\windows\microsoft.net\framework\v4.0.30319\InstallUtil.exe -i ".EXE file of window service"

相关问题