zoukankan      html  css  js  c++  java
  • C#代码安装Windows服务

    using System;
    using System.Collections.Generic;
    using System.ServiceProcess;
    using System.Configuration.Install;

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // 运行服务
            if (args.Length == 0)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new MyService1() };
                ServiceBase.Run(ServicesToRun);
            }
            // 安装服务
            else if (args[0].ToLower() == "/i" || args[0].ToLower() == "-i")
            {
                try
                {
                    string[] cmdline = { };
                    string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;

                    TransactedInstaller transactedInstaller = new TransactedInstaller();
                    AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
                    transactedInstaller.Installers.Add(assemblyInstaller);
                    transactedInstaller.Install(new System.Collections.Hashtable());
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
            }
            // 删除服务
            else if (args[0].ToLower() == "/u" || args[0].ToLower() == "-u")
            {
                try
                {
                    string[] cmdline = { };
                    string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;

                    TransactedInstaller transactedInstaller = new TransactedInstaller();
                    AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
                    transactedInstaller.Installers.Add(assemblyInstaller);
                    transactedInstaller.Uninstall(null);
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
            }
        }
    }

  • 相关阅读:
    微软推出Silverlight 5.2版本
    C# 安装部署,关于自定义操作,不能被执行。
    SQL_server_2000安装时挂起的解决办法,其实很简单。
    最全Microsoft JET Database Engine(0x80004005)未指定错误的解决方法
    WP博客wordpress,robots.txt写法
    2012年Web设计和开发的15个趋势
    SEO优化应该如何使用nofollow属性
    WordPress如何做SEO?WordPress博客怎样做网站优化。
    windowsXP SP3安装Photoshop CS 3字体输入没反应的问题及解决办法
    打开网页 错误 2147467259 : 错误描述: 未指定的错误 IIS 限XP系统
  • 原文地址:https://www.cnblogs.com/sntetwt/p/8888734.html
Copyright © 2011-2022 走看看