创建windows服务基本教程.docx_第1页
创建windows服务基本教程.docx_第2页
创建windows服务基本教程.docx_第3页
创建windows服务基本教程.docx_第4页
创建windows服务基本教程.docx_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的。所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Windows Service写很深入。本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。一、创建一个Windows Service1)创建Windows Service项目2)对Service重命名将Service1重命名为你服务名称,这里我们命名为ServiceTest。二、创建服务安装程序1)添加安装程序之后我们可以看到上图,自动为我们创建了ProjectInstaller.cs以及2个安装的组件。2)修改安装服务名右键serviceInsraller1,选择属性,将ServiceName的值改为ServiceTest。3)修改安装权限右键serviceProcessInsraller1,选择属性,将Account的值改为LocalSystem。三、写入服务代码1)打开ServiceTest代码右键ServiceTest,选择查看代码。2)写入Service逻辑添加如下代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.ServiceProcess;usingSystem.Text;namespaceWindowsServiceTestpublicpartialclassServiceTest : ServiceBasepublicServiceTest()InitializeComponent();protectedoverridevoidOnStart(string args)using(System.IO.StreamWriter sw = newSystem.IO.StreamWriter(C:log.txt, true)sw.WriteLine(DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss ) + Start.);protectedoverridevoidOnStop()using(System.IO.StreamWriter sw = newSystem.IO.StreamWriter(C:log.txt, true)sw.WriteLine(DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss ) + Stop.);这里我们的逻辑很简单,启动服务的时候写个日志,关闭的时候再写个日志。四、创建安装脚本在项目中添加2个文件如下(必须是ANSI或者UTF-8无BOM格式):1)安装脚本Install.bat?123%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe WindowsServiceTest.exeNet Start ServiceTestsc config ServiceTest start= auto2)卸载脚本Uninstall.bat?1%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /uWindowsServiceTest.exe3)安装脚本说明第二行为启动服务。第三行为设置服务为自动运行。这2行视服务形式自行选择。4)脚本调试如果需要查看脚本运行状况,在脚本最后一行加入pause五、在C#中对服务进行控制0)配置目录结构简历一个新WPF项目,叫WindowsServiceTestUI,添加对System.ServiceProcess的引用。在WindowsServiceTestUI的binDebug目录下建立Service目录。将WindowsServiceTest的生成目录设置为上面创建的Service目录。生成后目录结构如下图1)安装安装时会产生目录问题,所以安装代码如下:?12345678stringCurrentDirectory = System.Environment.CurrentDirectory;System.Environment.CurrentDirectory = CurrentDirectory + Service;Process process = newProcess();process.StartInfo.UseShellExecute = false;process.StartInfo.FileName = Install.bat;process.StartInfo.CreateNoWindow = true;process.Start();System.Environment.CurrentDirectory = CurrentDirectory;2)卸载卸载时也会产生目录问题,所以卸载代码如下:?12345678stringCurrentDirectory = System.Environment.CurrentDirectory;System.Environment.CurrentDirectory = CurrentDirectory + Service;Process process = newProcess();process.StartInfo.UseShellExecute = false;process.StartInfo.FileName = Uninstall.bat;process.StartInfo.CreateNoWindow = true;process.Start();System.Environment.CurrentDirectory = CurrentDirectory;3)启动代码如下:?12345usingSystem.ServiceProcess;ServiceController serviceController = newServiceController(ServiceTest);serviceController.Start();4)停止?123ServiceController serviceController = newServiceController(ServiceTest);if(serviceController.CanStop)serviceController.Stop();5)暂停/继续?12345678ServiceController serviceController = newServiceController(ServiceTest);if(serviceController.CanPauseAndContinue)if(serviceController.Status = ServiceControllerStatus.Running)serviceController.Pause();elseif(serviceController.Status = ServiceControllerStatus.Paused)serviceController.Continue();6)检查状态?12Service

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论