用c#创建windows服务完整教学
2011-04-24 11:06
369 查看
创建Windows服务
新建一个Windows服务项目,系统会自动生成一个Service1.cs
我们自己需要定义一个操作类,添加一个Operate.cs文件,在该文件中添加操作方法
在Service1该类中添加:
Timer是用来循环执行Operate.cs文件中操作方法的
在OnStart方法中输入:
手工建立一个方法,用来被Timer执行:
我们可以给项目中添加不同的操作类
安装
我们需要自定义一个安装。在解决方案中添加一个安装和部署中的安装项目
解决方案资源管理器中选择该项目,设置相关属性:
如Title、ProductName、Manufacturer(应用程序或组件制造商的名称,安装时为安装目录)、Author
然后在解决方案资源管理器中右键点击该项目,视图-自定义操作:
在安装、提交、回滚、卸载中分别添加“应用程序文件夹”中的主输出来自(上面建立的项目名称)活动项
再在解决方案资源管理器中右键点击安装项目,添加-项目输出:
添加主输出
在原项目中添加一个安装程序类,如ProjectInstaller.cs,双击这个安装程序类,设置者两个控件:
在serviceInstaller1控件属性窗口中设置属性内容,或者在代码中,查看InitializeComponent();方法
在private void InitializeComponent()中按如下修改:
设置描述Description 、显示名称DisplayName 、服务名称ServiceName
还有启动类型
新建一个Windows服务项目,系统会自动生成一个Service1.cs
我们自己需要定义一个操作类,添加一个Operate.cs文件,在该文件中添加操作方法
在Service1该类中添加:
Timer doTimer; Operate op1 = new Operate();
Timer是用来循环执行Operate.cs文件中操作方法的
在OnStart方法中输入:
//设置循环时间,以及循环执行方法 int doTimerNum = Convert.ToInt32(RestartMinute) * 60 * 1000; doTimer = new Timer(doAutoRestartNetReg, null, 0, doTimerNum);
手工建立一个方法,用来被Timer执行:
private void doAutoRestartNetReg(object values) { //循环调用方法 op1.RestartAppPool(AppPoolName, LogType); }
我们可以给项目中添加不同的操作类
安装
我们需要自定义一个安装。在解决方案中添加一个安装和部署中的安装项目
解决方案资源管理器中选择该项目,设置相关属性:
如Title、ProductName、Manufacturer(应用程序或组件制造商的名称,安装时为安装目录)、Author
然后在解决方案资源管理器中右键点击该项目,视图-自定义操作:
在安装、提交、回滚、卸载中分别添加“应用程序文件夹”中的主输出来自(上面建立的项目名称)活动项
再在解决方案资源管理器中右键点击安装项目,添加-项目输出:
添加主输出
在原项目中添加一个安装程序类,如ProjectInstaller.cs,双击这个安装程序类,设置者两个控件:
在serviceInstaller1控件属性窗口中设置属性内容,或者在代码中,查看InitializeComponent();方法
在private void InitializeComponent()中按如下修改:
{ this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); // // serviceProcessInstaller1 // this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; // // serviceInstaller1 // this.serviceInstaller1.Description = "本服务用于IIS应用程序池自定义操作"; this.serviceInstaller1.DisplayName = "IISAppPool Service"; this.serviceInstaller1.ServiceName = "IISAppPoolService"; this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; // // ProjectInstaller // this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.serviceProcessInstaller1, this.serviceInstaller1}); }
设置描述Description 、显示名称DisplayName 、服务名称ServiceName
还有启动类型
相关文章推荐
- C# windows服务:创建Windows服务(Windows Services)的一般步骤
- C#创建Windows Service(Windows 服务)基础教程
- C#创建Windows Service(Windows 服务)基础教程
- c#创建Windows service (Windows 服务)基础教程
- C# 创建Windows Service服务 Windows服务
- C#实现windows服务安装,服务名可配置时出问题(无法创建 ProjectInstaller 安装程序类型的实例)
- C#创建Windows Service(Windows 服务)基础教程
- .NET创建Windows服务详细步骤(C#)
- 使用C#创建windows 服务
- [Solution] Microsoft Windows 服务(1) C#创建Windows服务
- C#创建windows服务的步骤
- C# windows 服务 创建 以及调试
- C#创建Windows Service(Windows 服务)基础教程
- C# 利用 Windows服务模板 创建、安装与卸载Windows服务
- C# 自动运行代码 (创建windows 服务的形式 )
- C# windows服务的创建与调试
- C#创建Windows服务入门图解(VS2010)
- C#创建Windows Service(Windows 服务)基础教程
- C# 创建 Windows 服务
- C# 创建、安装和卸载Windows服务程序