zoukankan      html  css  js  c++  java
  • Bat file 安装和卸载同级目录下的.net 服务

    今天得到个需求 客户需要用batch file 安装和卸载服务,网上搜了一把例子,都只解决了单个问题,我来稍微总结一下

    安装服务

    @ECHO OFF

    REM The following directory is for .NET 4.0
    set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
    set PATH=%PATH%;%DOTNETFX4%

    echo Installing My Win Service...
    echo ---------------------------------------------------
    %DOTNETFX4%\InstallUtil "%~dp0MyService.exe"
    echo ---------------------------------------------------
    pause
    echo Done.

    卸载服务

    @ECHO OFF

    REM The following directory is for .NET 4.0
    set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
    set PATH=%PATH%;%DOTNETFX4%

    echo UnInstalling My Win Service...
    echo ---------------------------------------------------
    %DOTNETFX4%\InstallUtil /u "%~dp0MyService.exe"
    echo ---------------------------------------------------
    pause
    echo Done.

    这里%~dp0是关键,目的是找和bat file同一目录下的文件,不然安装会到“C"\windows\system32”下面去找你的服务exe文件

    http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work

    想写得更好一点可以参看下面网址的solution2

    http://www.codeproject.com/Questions/505250/HowplustoplusInstallplusorplusUninstallplusWindows

    给同一个 service 用不同的别名安装多次可以参看,(因为网址被国内屏蔽了,所以我把原文给贴了过来 ^_^)

    http://journalofasoftwaredev.wordpress.com/2008/07/16/multiple-instances-of-same-windows-service/

    Multiple instances of same windows service
    Filed under: .net, advice, code example, tips — Tags: .net, code example, multiple instances, windows services — Michael Cromwell @ 7:36 pm
    There are times were you want to be able install multiple instances of the same windows service onto the same server (i.e. to support different environments but you have limited servers at your diposal) this poses a problem, you are probably aware windows will not allow more than 1 windows service to be installed with the same service name and when you create a windows service project and an installer you assign the service name at design time.
    We need is someway to assign the service name at runtime, well after some extensive googling around I found a way of achieving this, the ProjectInstaller has 2 methods you can override Install and Uninstall which enables us to make changes to the service installer at the at runtime  Yeah that’s great however how do we assign the service name?! Fear not! at our disposal is the Context class that happens to have a Parameters dictionary so we can now capture custom command arguments passed to installutil.exe, enough yapping time for some code:

    public override void Install(System.Collections.IDictionary stateSaver)
    {
        RetrieveServiceName();
        base.Install(stateSaver);
    }
     
    public override void Uninstall(System.Collections.IDictionary savedState)
    {
        RetrieveServiceName();
        base.Uninstall(savedState);
    }
     
    private void RetrieveServiceName()
    {
        var serviceName = Context.Parameters["servicename"];
        if (!string.IsNullOrEmpty(serviceName))
        {
            this.SomeService.ServiceName = serviceName;
            this.SomeService.DisplayName = serviceName;
        }
    }
    In this instance I have made the servicename argument optional in which case it will use the default assigned at design time, you could enforce it in your version.

    We can now use this like this:
    installutil /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe
    and for uninstall:
    installutil /u /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe

  • 相关阅读:
    每日leetcode-数组-506. 相对名次
    每日leetcode-数组-412. Fizz Buzz
    每日leetcode-数组-299. 猜数字游戏
    正则表达式
    Linux三剑客之grep
    Google 74版本上传附件没有“选择文件”按钮
    Google卸载后再次安装提示安装失败
    linux下,数据泵导dmp文件
    Oracle数据库创建表空间
    Lr controller运行时,报错missing newline in C:userAdministratorDesktopjiekouusername.dat
  • 原文地址:https://www.cnblogs.com/michael703/p/3002031.html
Copyright © 2011-2022 走看看