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. Golang mgo 模糊查询的使用

    在日常使用的Mongodb中,有一项功能叫做模糊查询(使用正则匹配),例如: db.article.find({"title": {$regex: /a/, $options: & ...

  2. 其实每个行业都有各自的辛苦,好的程序员并不累,他们乐此不疲(见过太多在职位事业、人生方向上随转如流的人,累了疲乏了就去做别的事情了。必须有自己的坚守和立足的点,自我驱动,否则沦为在别人的体制制度中被驱赶一生)good

    作者:陈柯好链接:https://www.zhihu.com/question/39813913/answer/104275537来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  3. Redis的bind的误区(转)

    原文1:https://blog.csdn.net/cw_hello1/article/details/83444013 原文2:https://www.cnblogs.com/suiyueqiann ...

  4. sublimeText3汉化安装教程 附注册码

    sublimeText3汉化安装教程 sublimeText3 很不错,前面几天下了vscore学习Node.js,感觉有点懵,今天下载sublimeText3,遇到的一些小问题,在这里说说: 百度云 ...

  5. c#基础知识梳理(四)

    上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10826971.html 一.类 当你定义一个类时,你定义了一个数据类型的蓝图.这实际上并没有定义任何的数据, ...

  6. ZROI17普及23-B星空题解--图的灵活转化

    题目链接 版权原因不予提供 分析 这题思路很妙啊,虽然已经算半个套路题(因为我太菜了) 将框视为点,若一个球能放在\(x\)或\(y\)框,则\(x,y\)连一条无向边.有一条非常显然的性质是:在联通 ...

  7. Go part 1 初探

    Go 语言简介 Go 语言是 Google 在2007年开发的一种开源编程语言,于2009年11月10日向全球公布 出自 Ken Thompson 和 Rob Pike.Robert Grieseme ...

  8. Layui 实现input 输入和选择

    <div class="layui-col-md4"> <label class="layui-form-label">移交单位< ...

  9. # 机器学习算法总结-第九天(XGboost)

  10. 装机篇:ubuntu 14.04 在英文环境下安装中文输入法(转载)

    ubuntu默认的输入法是ibus,综合网上评论,fcitx的支持者更多,而且个人感觉fcitx也的确不错,可以满足日常输入. STEP1: 在Ubuntu Software Center 搜索fci ...