WINDOWS服务安装(C#)
来源:互联网 发布:西安交大acca知乎 编辑:程序博客网 时间:2024/06/04 17:47
1、新建服务项目
比如
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Windows.Forms;
namespace CatchArticle
{
public class CatchArticle : System.ServiceProcess.ServiceBase
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private Thread MainThread;
public CatchArticle()
{
// 该调用是 Windows.Forms 组件设计器所必需的。
InitializeComponent();
MainThread=new Thread(new ThreadStart(ThreadFunc));
MainThread.Priority=ThreadPriority.Lowest;
}
// 进程的主入口点
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CatchArticle() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "CatchArticle";
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 设置具体的操作,以便服务可以执行它的工作。
/// </summary>
protected override void OnStart(string[] args)
{
MainThread.Start();
}
/// <summary>
/// 停止此服务。
/// </summary>
protected override void OnStop()
{
MainThread.Abort();
}
public static void ThreadFunc()
{
int LastHour=DateTime.Now.Hour;
while (true)
{
System.Threading.Thread.Sleep(1000);
if (DateTime.Now.Hour-1==LastHour)
{
MessageBox.Show("为了爱护您的眼睛,请您暂时休息5分钟并向远处眺望!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
LastHour=DateTime.Now.Hour;
}
}
}
}
}
========================================
2、然后增加一个ProjectInstall.cs 继承自Installer ,里面的一些参数可以自己定义
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;
namespace CatchArticle
{
/// <summary>
/// myInstall 的摘要说明。
/// </summary>
///
[RunInstaller(true)]
public class myInstall : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public myInstall()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "CatchArticle";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}
3、然后便以Release
4、使用C:\......\Framework\v1.1..\installUtil.exe C:\项目路径\bin\release\TestService.exe
注意:如果不增加ProjectInstall.cs,则会报如下错误
正在安装程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
受影响的参数是:
assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
正在提交程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
受影响的参数是:
assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
没有安装程序,因此移除 InstallState 文件。
正在安装程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
受影响的参数是:
assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
正在提交程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
受影响的参数是:
assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
没有安装程序,因此移除 InstallState 文件。
- WINDOWS服务安装(C#)
- C#Windows 服务制作安装删除
- vs2008 windows服务(C#) 开发与安装
- vs2008 windows服务(C#) 开发与安装
- vs2008 windows服务(C#) 开发与安装
- vs2008 windows服务(C#) 开发与安装
- vs2008 windows服务(C#) 开发与安装
- 编写windows 服务(c++)
- Windows 服务安装(备忘)
- Solr服务安装(Windows)
- C语言实现Windows服务并进行安装部署
- 制作windows服务安装服务
- 编写Windows服务(C#)
- C#Windows服务介绍
- C#Windows服务
- C#windows service服务
- C#Windows服务
- 创建Windows服务(C++)
- 上传 文本文件 ,jpg
- 基于x86和JVM浅谈32bit与64bit的区别
- sql server 2005 还原备份数据库
- 提高JAVA的性能的几种方法
- Highcharts使用指南
- WINDOWS服务安装(C#)
- Settings: android 组件如何响应语言变化
- Fusion实例2:如何通过竞技场使用RPC(远程过程调用)? .
- WordPress插件机制实现原理
- Xcode4.4 添加Framework方法
- (二)c++builder基础知识之在c++builder中由一个form上的按钮打开到另一个form界面
- 爱是永久相思情
- 3.排序--快速排序
- BufferedReader与InputStream的心得