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

    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;
                }
            }
        }
    }

  • 相关阅读:
    java爬虫(八)使用node.js获取network中api接口内信息并用java的jsoup重写该方法
    java爬虫(七)使用httpclient模拟浏览器GET,POST
    Java selenium对cookies的操作
    java爬虫(六)分析AJAX接口获取网页动态内容
    java爬虫(五)利用selenium 模拟点击获取动态页面的内容
    java爬虫(四)利用Jsoup获取需要登陆的网站中的内容(无验证码的登录)
    java爬虫(三)利用HttpClient和Jsoup模拟网页登陆(无验证码)
    Java远程服务器调优
    20 vue-router的使用
    19 关于Vue中main.js,App.vue,index.html之间关系进行总结
  • 原文地址:https://www.cnblogs.com/tanshupeng/p/3899919.html
Copyright © 2011-2022 走看看