用C#语言编写Windows服务程序的七个步骤
来源:互联网 发布:python for in range 编辑:程序博客网 时间:2024/06/01 07:19
用.NET写WINDOWS 服务
首先用VS2005、2010创建Windows服务项目。系统会自动创建Service1.cs文件,手动将其删除。
第二步"添加"-->"新建项"-->选择"windows服务"命名为ServerDemo.cs单击确定。
第三步"添加"-->"新建项"-->选择"安装程序类"命名为ServerDemoInstaller.cs单击确定。
第四步 打开ServerDemoInstaller.Designer.cs代码如下:
partial class ServerDemoInstaller
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller;
private System.ServiceProcess.ServiceInstaller serviceInstaller;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{ if (disposing && (components != null))
{ components.Dispose();
} base.Dispose(disposing);
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller
//
this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;
this.serviceProcessInstaller.Password = null;
this.serviceProcessInstaller.Username = null;
//
// serviceInstaller
//
this.serviceInstaller.ServiceName = "服务名称";
this.serviceInstaller.Description = "服务显示描述";
this.serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller,
this.serviceInstaller});
}
}
partial class ServerDemo : ServiceBase
{ public DeleteFiles()
{ InitializeComponent();
}
private System.Timers.Timer timerOfDeleteFile;
private StreamWriter sw = null;
private bool canStart = true;
///
/// 间隔周期毫秒
///
public int IntervalTime= Convert.ToInt32(1000);
protected override void OnStart(string[] args)
{
this.timerOfDeleteFile = new System.Timers.Timer();
this.timerOfDeleteFile.Elapsed += new System.Timers.ElapsedEventHandler(this.test_Elapsed);
this.timerOfDeleteFile.Start();
}
void test_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.sw = new StreamWriter("d://pengfuDeleteFileLog.txt", true);
this.sw.WriteLine("服务启动!");
if (!this.canStart)
{ return;
}
this.canStart = false;
this.sw.WriteLine("服务被禁用!");
this.canStart = true;
this.sw.WriteLine("服务被启动!");
this.sw.Flush();
this.sw.Close();
}
protected override void OnStop()
{
this.timerOfDeleteFile.Close();
this.sw.Dispose();
}
}
第六步 服务安装
C:WindowsMicrosoft.NETFrameworkv2.0.50727InstallUtil.exe安装工具
InstallUtil 服务程序路径(win服务编译生成后在debug/release目录下)
第七步 服备卸装
InstallUtil -u 服务程序路径
服务安装后右键“我的电脑”-->管理-->服务中找到刚安装的win服务。右键启动即可。
转载自
http://www.5ishequ.com/community/title/view/5bff2b860264a289d8cdf5a515761a4/page1.html
- 用C#语言编写Windows服务程序的七个步骤
- C++编写Windows服务程序 用 C 语言编写 Windows 服务程序的五个步骤
- 用 C 语言编写 Windows 服务程序的五个步骤
- 用 C 语言编写 Windows 服务程序的五个步骤
- 用C语言编写Windows服务程序的五个步骤
- 用 C 语言编写 Windows 服务程序的五个步骤
- 用C 语言编写 Windows 服务程序的五个步骤
- 用C语言编写Windows服务程序的五个步骤
- 用C语言编写Windows服务程序的五个步骤
- 用C语言编写Windows服务程序的五个步骤
- 用C语言编写Windows服务程序的五个步骤
- 用C语言编写Windows服务程序的五个步骤
- 用 C 语言编写 Windows 服务程序的五个步骤
- 用C语言编写Windows服务程序的五个步骤
- 用 C 语言编写 Windows 服务程序的五个步骤
- 用C语言编写Windows服务程序的五个步骤
- 用 C 语言编写 Windows 服务程序的五个步骤
- 用 C 语言编写 Windows 服务程序的五个步骤
- 软件项目经理新手上路(5) - 头痛医头,脚痛医脚
- 用EXCEL快速定位盛大安全卡的内容
- Android之SAX解析XML文件
- 对iframe数据的保存与读取
- 安装MySQL之后修改编码方式
- 用C#语言编写Windows服务程序的七个步骤
- 软件项目经理新手上路(6) - 不要进行小改进
- 解决firefox不支持window.event.returnValue = false
- mod..........
- 软件项目经理新手上路(7) - 再序 - 多点儿人味儿
- 项目的故事
- Axis2体系结构
- MFC数组类
- 关于编程 关于开发