Batch Script to Install or Uninstall a .NET Windows Service

后端 未结 10 1558
我在风中等你
我在风中等你 2021-01-31 05:13

I have no experience writing batch scripts, but I was wondering if there was a way to install a .NET Windows service using installutil.exe using such a script, or u

相关标签:
10条回答
  • 2021-01-31 05:55

    I have found that it is always better to use a good install project that to use batch files for installing an app. There are times though that that can't be done. Several projects at work were written in the days of Windows NT and early Windows XP and use simple batch files for installation. During those times, converting the batch file to an install packed is more trouble than a simple tweak. Through much searching, I have found that http://ss64.com/nt/ is a very good Windows batch file reference. (It just feels strange, with all our advancement in software technolgies, to have to write that last sentence.)

    Anyway, Happy Coding! - regardless of the "language".

    0 讨论(0)
  • 2021-01-31 05:57

    Suggestions:

    • Make use of the environment, Windows may not be installed on C:. But you can use %WinDir%.
    • You can redirect echo to append to a file:

      echo A message >> logfile.txt

    • Keeping track of everything and convering all the edge cases can be challenging in cmd.exe, it is not a rich environment.

    • There is no consistent place for documentation. But help (from the command line) on "cmd", "if", "for", "set" and "call" covers much of avaialble syntax.
    • Set echo off at the start to see the commands as they are executed.
    0 讨论(0)
  • 2021-01-31 05:57
    @echo off
    
    SET PROG="c:\YourServiceLocation\Service.exe" SET FIRSTPART=%WINDIR%"\Microsoft.NET\Framework\v" SET SECONDPART="\InstallUtil.exe" SET DOTNETVER=4.0.30319 IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install
    
    GOTO fail :install ECHO Found .NET Framework version %DOTNETVER% ECHO Installing service %PROG% %FIRSTPART%%DOTNETVER%%SECONDPART% %PROG% GOTO end :fail echo FAILURE -- Could not find .NET Framework install :param_error echo USAGE: installNETservie.bat [install type (I or U)] [application (.exe)] :end ECHO DONE!!! Pause
    

    run this bat file as administrator

    0 讨论(0)
  • 2021-01-31 05:58

    It is easier to just make self-installing services. Once you implement this, you can either run the service exe directly with the (/i or /u switch), or wrap the call in a batch file if you'd like.

    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            //Install service
            if (args[0].Trim().ToLower() == "/i")
            { System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "/i", Assembly.GetExecutingAssembly().Location }); }
    
            //Uninstall service                 
            else if (args[0].Trim().ToLower() == "/u")
            { System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); }
        }
        else
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
    }
    
    0 讨论(0)
提交回复
热议问题