




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Module7 Unit1 He spent about twenty-one hours in space(教学设计)-2023-2024学年外研版(三起)英语六年级下册
- 华北理工大学冀唐学院《研究型建筑设计》2023-2024学年第二学期期末试卷
- 山西国际商务职业学院《计算机组成原理理论》2023-2024学年第二学期期末试卷
- 哈尔滨铁道职业技术学院《班级活动的组织》2023-2024学年第二学期期末试卷
- 辽宁民族师范高等专科学校《机电系统设计与控制》2023-2024学年第二学期期末试卷
- 广州东华职业学院《海洋生物技术综合实验》2023-2024学年第二学期期末试卷
- 南昌大学科学技术学院《新编大学生安全教育》2023-2024学年第二学期期末试卷
- 河北科技师范学院《西方财务会计双语》2023-2024学年第二学期期末试卷
- 共青科技职业学院《学前儿童保育学》2023-2024学年第二学期期末试卷
- 陕西理工大学《数字信号处理》2023-2024学年第二学期期末试卷
- 北师大版 四年级下册心理健康教育 失败不可怕 |教案
- 中建企业建筑工程项目管理目标责任书(范本)
- 医师定期考核人文医学考试题库500题(含参考答案)
- 读书分享课件:《一句顶一万句》
- 物业消防安全管理培训【共54张课件】
- 空心杯电机基础知识
- DL-T+5839-2021土石坝安全监测系统施工技术规范
- 历年交管12123驾照学法减分复习题库带答案下载
- 人教鄂教版-科学-三年级下册-知识点
- 2024-2034年中国注射用赖氨匹林行业市场竞争格局及投资前景展望报告
- 供应链可持续采购实践
评论
0/150
提交评论