Self install windows service in .NET c#
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#的更多相关文章
- install windows service
install windows serivce e.g @echo offecho ---------------------------------------------------------- ...
- redis SERVER INSTALL WINDOWS SERVICE
以管理 员身份 运行 CMD 命令,进入redis所在目录,并运行下 脚本redis-server --service-install redis.windows-service.conf --log ...
- 使用C#编程语言开发Windows Service服务
转载-https://www.cnblogs.com/yubao/p/8443455.html Create Windows Service project using Visual Studio C ...
- Install Jenkins Slave as Windows Service
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Windows+service SC 直接创建windows s ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- 使用Windows Service Wrapper快速创建一个Windows Service
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
- Windows Service--Write a Better Windows Service
原文地址: http://visualstudiomagazine.com/Articles/2005/10/01/Write-a-Better-Windows-Service.aspx?Page=1 ...
- 使用Python写Windows Service服务程序
1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...
- C# 创建Windows Service
当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Servi ...
随机推荐
- (三十一)web 开发基础项目
1. 编写index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- ASP.NET Core 入门(4)(IIS 部署前后端站点)
.NET Core发布部署的文章园内有很多了,大家可以自行百度,该篇主要想总结需要注意的地方,列举前后端(比如前段 Vue,后端 WebAPI)在同一台服务器上的主要两种方式. 两种方式: 1. 前后 ...
- Uwl.Admin开源框架(一)
1.前言 作为一个忠实的软粉,一直期待微软出跨平台,一直在等待.Net Core,因为刚毕业对于.Net的很多东西不是很熟知,就开始了.Net Core的摸索,一路上坎坎坷坷,对于新技术一直很期待,就 ...
- Map集合中key不存在时使用toString()方法、valueOf()方法和强制转换((String))之间的区别
1.toString()方法 底层代码 public String toString() { return this; } 其返回值为String类型的字符串本身 Map<String, Obj ...
- export default和export的使用方式
在node中使用 var 名称=require('模块标识符') 来导入 module.exports 和exports 来暴露成员 在ES6中,也通过规范的形式,规定了ES6中如何导入和导出模块 E ...
- keras 切换后端 TensorFlow,cntk,theano
参考 https://keras.io/#configuring-your-keras-backend https://keras.io/backend/ Switching from one bac ...
- C++ 获取对象类型
在入门C++过程中,我们经常会遇到无法判断对象类型的情况. 头文件( VS编译器 ) #include <typeinfo> typeid(对象).name(); 例子: , &b ...
- CRM和C4C里的组织架构 - Organizational Structure
CRM(WebClient UI) CRM(SAP GUI,事务码PPOMA_CRM) C4C 以列表方式显示: 以图形方式显示: UI模型: /SAP_BYD_APPLICATION_UI/mom/ ...
- Sliverlight/WPF 样式使用方法
1,UserControl 页面级样式: UserControl.Resources style setter Property value. TargetType为应用的类型 <UserCon ...
- 从win到多系统
相信有不少电脑爱好者喜欢折腾系统,尤其还是一个小白(感觉多系统强的不要不要的,各种崇拜),然后就走上了深渊. 首先,我一开始也是个win系统的忠实用户,没用过其他系统的我几乎不知道其他系统的存在,反正 ...