




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
如何用c# 开发系统服务最近用.net开发了个隔时执行的小应用程序,登录MSDTC,放到服务器,运转良好,退出远程桌面,事情出来了,那个可爱的程序不运作了,然后我就搞了个任务计划,做了下,以为完事了,过了一些时间,发现不对劲,还是停止执行程序了,上去一看,又停止了,这时候我不得不重新考虑设置,还是写个系统服务就去吧,写上一段代码先写个服务再说。using System;using System.Collections;using System.ComponentModel;using System.Diagnostics;using System.ServiceProcess;using System.Configuration.Install;namespace CjjerTest.DotNet.ServiceDST / 应用程序 public class MyFirstService : System.ServiceProcess.ServiceBase public MyFirstService() this.CanPauseAndContinue = true; this.CanShutdown = true; this.CanHandleSessionChangeEvent = false; this.ServiceName = MyFirstService; protected override void OnStart(string args) protected override void OnStop() protected override void OnContinue() /启动 public static void Main() ServiceBase servicesToRun = new ServiceBase new MyFirstService(); ServiceBase.Run(servicesToRun); /安装 RunInstaller(true) public class ProjectInstaller : System.Configuration.Install.Installer private ServiceProcessInstaller myServiceProcessInstaller; private ServiceInstaller myServiceInstaller; public ProjectInstaller() this.myServiceProcessInstaller = new ServiceProcessInstaller(); this.myServiceInstaller = new ServiceInstaller(); / 安装 / 用户名 和 密码 this.myServiceProcessInstaller.Account = ServiceAccount.LocalSystem; this.myServiceProcessInstaller.Username = null; this.myServiceProcessInstaller.Password = null; / 服务名称,这样可以在net stop XX 里面使用了 / 启动类型 this.myServiceInstaller.ServiceName = MyFirstService; this.myServiceInstaller.StartType = ServiceStartMode.Automatic; / 加入 this.Installers.AddRange(new Installer this.myServiceProcessInstaller, this.myServiceInstaller); 其中在启动的代码中我们可以执行: protected override void OnStart(string args) StreamWriter sw=new StreamWriter(E:webwebnettestsernet.txt,true); sw.Write(rn另一条数据); sw.Close(); base.OnStart( args ); 或者创建另一条线程执行其他程序。1、编译 csc cservice.cs 2、安装 installutil cservice.exe 3、启动服务 net start MyFirstService 4、停止服务 net stop MyFirstService 5、卸载 installutil /u cservice.exe上次我专门说明了如何用c# 开发系统服务,由于很多时候installutil.exe这个工具用起来还是有点繁琐,一直想着用c#代码自己完成启动服务,删除服务的操作。这就需要调用.NET的DllImport,主要是调用advapi32.dll这个dll来实现。 关于advapi32.dll的接口,有兴趣的可以查。我这里就写出实现的C#代码。using System;using System.Runtime.InteropServices;namespace MyServiceInstallerclass ServiceInstallerpublic static string AppPathgetreturn System.Environment.CurrentDirectory;#region Private Variables/private string _servicePath;/private string _serviceName;/private string _serviceDisplayName;#endregion Private Variables#region DLLImportDllImport(advapi32.dll)public static extern IntPtr OpenSCManager(string lpMachineName,string lpSCDB, int scParameter);DllImport(Advapi32.dll)public static extern IntPtr CreateService(IntPtr SC_HANDLE,string lpSvcName,string lpDisplayName, int dwDesiredAccess,int dwServiceType,int dwStartType,int dwErrorControl,string lpPathName, string lpLoadOrderGroup,int lpdwTagId,string lpDependencies,string lpServiceStartName,string lpPassword);DllImport(advapi32.dll)public static extern void CloseServiceHandle(IntPtr SCHANDLE);DllImport(advapi32.dll)public static extern int StartService(IntPtr SVHANDLE,int dwNumServiceArgs,string lpServiceArgVectors);DllImport(advapi32.dll,SetLastError=true)public static extern IntPtr OpenService(IntPtr SCHANDLE,string lpSvcName,int dwNumServiceArgs);DllImport(advapi32.dll)public static extern int DeleteService(IntPtr SVHANDLE);DllImport(kernel32.dll)public static extern int GetLastError();#endregion DLLImport/ / 应用程序入口./ STAThreadstatic void Main(string args) int ExeId = (args=null)?0:0; if(args!=null & args.Length0) if(Int32.TryParse(args0,out ExeId) ; string svcName= DSIService; ServiceInstaller c = new ServiceInstaller(); if(ExeId=1) string svcPath; string svcDispName; /服务程序的路径 svcPath = System.IO.Path.Combine(AppPath,dsiSer.exe); svcDispName=DSIService; Console.WriteLine(执行的路径是0,svcPath); Console.WriteLine(创建的结果是0,c.InstallService(svcPath, svcName, svcDispName); else if(ExeId=2) Console.WriteLine(卸载的结果是0,c.UnInstallService(svcName); else Console.WriteLine(你要干什么?0,installutil /u dsiSer.exe); / / 安装和运行/ / 程序路径./ 服务名/ 服务显示名称./ 服务安装是否成功.public bool InstallService(string svcPath, string svcName, string svcDispName)#region Constants SC_MANAGER_CREATE_SERVICE = 0x0002;int SERVICE_WIN32_OWN_PROCESS = 0x00000010;/int SERVICE_DEMAND_START = 0x00000003;int SERVICE_ERROR_NORMAL = 0x00000001;int STANDARD_RIGHTS_REQUIRED = 0xF0000;int SERVICE_QUERY_CONFIG = 0x0001;int SERVICE_CHANGE_CONFIG = 0x0002;int SERVICE_QUERY_STATUS = 0x0004;int SERVICE_ENUMERATE_DEPENDENTS = 0x0008;int SERVICE_START =0x0010;int SERVICE_STOP =0x0020;int SERVICE_PAUSE_CONTINUE =0x0040;int SERVICE_INTERROGATE =0x0080;int SERVICE_USER_DEFINED_CONTROL =0x0100;int SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SERVICE_QUERY_CONFIG |SERVICE_CHANGE_CONFIG |SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS | SERVICE_START | SERVICE_STOP | SERVICE_PAUSE_CONTINUE | SERVICE_INTERROGATE | SERVICE_USER_DEFINED_CONTROL);int SERVICE_AUTO_START = 0x00000002;#endregion Constants declaration.tryIntPtr sc_handle = OpenSCManager(null,null,SC_MANAGER_CREATE_SERVICE);if (sc_handle.ToInt32() != 0)IntPtr sv_handle = CreateService(sc_handle,svcName,svcDispName,SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,svcPath,null,0,null,null,null);if(sv_handle.ToInt32() =0)CloseServiceHandle(sc_handle);return false;else/试尝启动服务int i = StartService(sv_handle,0,null);if(i=0)return false;CloseServiceHandle(sc_handle);return true;elsereturn false;catch(Exception e)throw e;/ / 反安装服务./ / 服务名.public bool UnInstallService(string svcName) int GENERIC_WRITE = 0x40000000; IntPtr sc_hndl = OpenSCManage
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025湖南张家界高新技术产业开发区管委会招聘公益性岗位工作人员1人考前自测高频考点模拟试题(含答案详解)
- 2025北京化工大学化学工程学院管理人员招聘1人模拟试卷及答案详解1套
- 2025年春季中国邮政储蓄银行云南省分行校园招聘考前自测高频考点模拟试题有完整答案详解
- 2025滇西科技师范学院公开招聘硕士研究生及以上和“双师型”教师(19人)模拟试卷附答案详解
- 2025广西河池市招聘紧缺学科教师118人模拟试卷及答案详解(全优)
- 2025贵州铜仁开放大学引进专业技术人才3人模拟试卷附答案详解(模拟题)
- 2025年陕西大秦电能集团有限公司安康大禹公司招聘(1人)考前自测高频考点模拟试题带答案详解
- 2025河南安阳市疾病预防控制中心招聘15人考前自测高频考点模拟试题附答案详解(黄金题型)
- 2025河南省投资促进中心招聘1人考前自测高频考点模拟试题附答案详解(考试直接用)
- 2025海南保亭黎族苗族自治县市场监督管理局公益性岗位人员招聘1人考前自测高频考点模拟试题附答案详解
- 2025年时事政治试题库及答案(共550题)
- 汽车玻璃升降器培训资料
- DB2301∕T 178-2024 地下市政基础设施普查及信息化管理平台建设技术规程
- 中医肠道健康课件
- 妊娠期高血压疾病诊治指南(2025版)解读
- 2024年江苏南通中考满分作文《前进我有我的姿态》13
- 行前说明会流程
- 人教版七年级历史下册各单元测试题(全套,含答案)
- 《另眼观察》(课件)-2024-2025学年沪书画版五四学制(2024)美术六年级上册
- 2023部编新人教版五年级(上册)道德与法治全册教案
- 体育运动概论1
评论
0/150
提交评论