http://stackoverflow.com/questions/4144019/self-install-windows-service-in-net-c-sharp

using System;
using System.Configuration.Install;
using System.Reflection;
using System.ServiceProcess;
using System.IO; namespace ConsoleApplication1
{
class Program : ServiceBase
{
static void Main(string[] args)
{ AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; if (System.Environment.UserInteractive)
{
string parameter = string.Concat(args);
switch (parameter)
{
case "--install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "--uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
else
{
ServiceBase.Run(new Program());
} } private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
File.AppendAllText(@"C:\Temp\error.txt", ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message);
} public Program()
{
this.ServiceName = "My Service";
File.AppendAllText(@"C:\Temp\sss.txt", "aaa"); } protected override void OnStart(string[] args)
{
base.OnStart(args); File.AppendAllText(@"C:\Temp\sss.txt", "bbb");
} protected override void OnStop()
{
base.OnStop(); File.AppendAllText(@"C:\Temp\sss.txt", "ccc");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text; namespace ConsoleApplication1
{
[RunInstaller(true)]
public class MyWindowsServiceInstaller : Installer
{
public MyWindowsServiceInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller(); //set the privileges
processInstaller.Account = ServiceAccount.LocalSystem; serviceInstaller.DisplayName = "My Service";
serviceInstaller.StartType = ServiceStartMode.Automatic; //must be the same as what was set in Program's constructor
serviceInstaller.ServiceName = "My Service";
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}
}
}

Self install windows service in .NET c#的更多相关文章

  1. install windows service

    install windows serivce e.g @echo offecho ---------------------------------------------------------- ...

  2. redis SERVER INSTALL WINDOWS SERVICE

    以管理 员身份 运行 CMD 命令,进入redis所在目录,并运行下 脚本redis-server --service-install redis.windows-service.conf --log ...

  3. 使用C#编程语言开发Windows Service服务

    转载-https://www.cnblogs.com/yubao/p/8443455.html Create Windows Service project using Visual Studio C ...

  4. Install Jenkins Slave as Windows Service

    https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Windows+service SC 直接创建windows s ...

  5. C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  6. 使用Windows Service Wrapper快速创建一个Windows Service

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

  7. Windows Service--Write a Better Windows Service

    原文地址: http://visualstudiomagazine.com/Articles/2005/10/01/Write-a-Better-Windows-Service.aspx?Page=1 ...

  8. 使用Python写Windows Service服务程序

    1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...

  9. C# 创建Windows Service

    当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Servi ...

随机推荐

  1. python第五天---集合与format格式化

    """ 集合:set 1.由不同元素组成, 2.无序 3.不可变:数字.字符串.元组 不可变类型 """ s = {1, 2, 3, 4, ...

  2. 1231: 删除字符串中指定的字符(Java)

    WUSTOJ 1231: 删除字符串中指定的字符 题目 原题链接 Description 明天就要英语考试了,小明明正在挑灯夜战背单词.小明明发现单词很难背,背一个忘一个.经过仔细研究,小明明发现单词 ...

  3. 【转】利用匿名namespace解决C++中重复定义的问题

    目录 利用匿名namespace解决C++中重复定义的问题 原文:https://blog.csdn.net/pi9nc/article/details/11267031 利用匿名namespace解 ...

  4. asp.net core-14.JWT认证授权 生成 JWT Token

    源码下载 语言组织能力不好 ,看这个 视频 用visual studio code打开文件,运行dotnet watch run 之后在postman里面去访问 拿到Token后

  5. SpringCloud Hystrix 参数

    hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey Command PropertiesExecution ...

  6. springboot中使用验证码kaptcha

    1.pom.xml引入kaptcha所需要的jar包 <!-- 验证码 --> <dependency> <groupId>com.github.penggle&l ...

  7. Effective Java 读书笔记(二):对象通用方法

    1 重写equals方法时请遵守通用约定 (1)无需覆盖equals方法的情况 要求独一无二 不要求逻辑相等 超类已经覆盖equals方法,对其子类也适用 一个类是私有的或者是包私有(可以重写后抛出异 ...

  8. JDBC 学习复习8 C3P0数据源使用

    C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. c3p0与dbcp区别 dbcp ...

  9. PHP函数问题

    有时候,运行nginx和PHP CGI(PHP FPM)web服务的Linux服务器,突然系统负载上升,用top命令查看,很多phpcgi进程的CPU利用率接近100%后来通过跟踪发现,这种情况与PH ...

  10. python3爬虫之requests库基本使用

    官方文档链接(中文) https://2.python-requests.org/zh_CN/latest/ requests  基于  urllib3 ,python编写. 安装 pip insta ...