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

C#代码安装Windows服务的更多相关文章

  1. C#代码安装Windows服务(控制台应用集成Windows服务)

    最近在为一款C/S架构的科研软件开发云计算版,需要用到WCF,考虑到不需要什么界面以及稳定性,无人值守性,准备用Windows Service作为宿主,无奈Windows Service的安装太为繁复 ...

  2. 安装windows服务批处理代码

    批处理是DOS时代比较常用的方法之一,目前来说也是一种高效的方法,复制代码到文本文件中,保存并修改文件扩展名为“*.bat”. 安装windows服务批处理代码如下: @echo off set fi ...

  3. .net安装windows服务配置文件config

    .net安装windows服务 : 在windows服务的项目(WindowsService1)代码文件中有一个app.config 配置文件,可以通过此文件进行时间等的更改而无需重新生成项目:那我们 ...

  4. 无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer、Windows服务器管理工具或NET START命令启动它

    无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer.Windows服务器管理工具或NET START命令启动它 1. ...

  5. .net安装windows服务和生产webservice

    安装windows服务 1.打开cmd对话框. 2.输入 cd WINDOWS\Microsoft.NET\Framework64\v2.0.50727\ 3.回车 4.输入InstallUtil.e ...

  6. TopSelf安装Windows服务提示:执行未经授权的操作。。

    在一个项目中用到了八九个服务,服务的执行时间也是五花八门,有的年末执行一次,有的月中执行一次,有的月末最后一天执行一次,有的月初连续执行5天, 有的每天晚上执行,...还好各个服务并没有严格的关联关系 ...

  7. mysql下载和安装Windows服务

    一.下载mysql:https://dev.mysql.com/downloads/mysql/,解压拷贝到D:\software\mysql-8.0.13-winx64 二.在D:\software ...

  8. c# 安装windows服务

    C# windows服务: 第一种 :通过cmd命令安装.卸载.启动和停止Windows Service(InstallUtil.exe) 步骤: 1.运行--〉cmd:打开cmd命令框 2.在命令行 ...

  9. 创建、设置和安装Windows服务

    文章大部分内容转自:http://www.cnblogs.com/greatandforever/archive/2008/10/14/1310504.html:和:http://www.cnblog ...

随机推荐

  1. python接口自动化测试二十五:执行所有用例,并生成HTML测试报告

        import requestsimport unittest class TestQQ(unittest.TestCase):    '''测试QQ号接口'''      # 此注释将展示到测 ...

  2. python接口自动化测试十八:使用bs4框架爬取图片

    # 爬图片# 目标网站:http://699pic.com/sousuo-218808-13-1.htmlimport requestsfrom bs4 import BeautifulSoupimp ...

  3. python接口自动化测试十七:使用bs4框架进行简单的爬虫

    安装:beautifulsoup4 from bs4 import BeautifulSoup yoyo = open('yoyo.html', 'r')   # 以读的方式打开“yoyo.html” ...

  4. webpack 使用环境变量

    要在开发和生产构建之间,消除 webpack.config.js 的差异.你可能需要环境变量. 可以使用 Node.js 模块的标准方式:在运行 webpack 时设置环境变量,并且使用 Node.j ...

  5. spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  6. PIGS

    题解: 考虑建立一个分层图,从s向猪圈连边,容量为初始容量, 猪圈向第一个顾客连边,容量为INF 顾客向汇点连边,容量为购买量 这样一轮就搞完了,考虑下一个顾客 由于上一轮被顾客访问的猪圈都互通了,那 ...

  7. layer弹框插件使用

    需要在jquery之后导入 <link rel="stylesheet" href="${pageContext.request.contextPath }/js/ ...

  8. Codeforces Round #319 (Div. 2) E - Points on Plane

    题目大意:在一个平面里有n个点,点坐标的值在1-1e6之间,让你给出一个遍历所有点的顺序,要求每个点走一次,且 曼哈顿距离之和小于25*1e8. 思路:想了一会就有了思路,我们可以把1e6的x,y坐标 ...

  9. Matrix Power Series POJ3233

    递推思想  先放着 见 https://www.cnblogs.com/jackge/p/3147604.html

  10. ie浏览器的版本

    原文链接:https://www.cnblogs.com/XCWebLTE/archive/2017/06/15/7017338.html function IEVersion() { var use ...