本文引见了运用C#建立Windows效劳的实例代码,分享给人人
一、开辟环境
操作体系:Windows 10 X64
开辟环境:VS2015
编程言语:C#
.NET版本:.NET Framework 4.0
目的平台:X86
二、建立Windows Service
1、新建一个Windows Service,并将项目名称改成“MyWindowsService”,以下图所示:
2、在解决方案资源管理器内将Service1.cs改成MyService1.cs后并点击“检察代码”图标按钮进入代码编辑器界面,以下图所示:
3、在代码编辑器内如入以下代码,以下所示:
using System; using System.ServiceProcess; using System.IO; namespace MyWindowsService { public partial class MyService : ServiceBase { public MyService() { InitializeComponent(); } string filePath = @"D:\MyServiceLog.txt"; protected override void OnStart(string[] args) { using (FileStream stream = new FileStream(filePath,FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now},效劳启动!"); } } protected override void OnStop() { using (FileStream stream = new FileStream(filePath, FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now},效劳住手!"); } } } }
4、双击项目“MyWindowsService”进入“MyService”设想界面,在空缺位置右击鼠标弹出上下文菜单,选中“增加装置顺序”,以下图所示:
5、此时软件会生成两个组件,分别为“serviceInstaller1”及“serviceProcessInstaller1”,以下图所示:
6、点击“serviceInstaller1”,在“属性”窗体将ServiceName改成MyService,Description改成我的效劳,StartType坚持为Manual,以下图所示:
7、点击“serviceProcessInstaller1”,在“属性”窗体将Account改成LocalSystem(效劳属性体系级别),以下图所示:
8、鼠标右键点击项目“MyWindowsService”,在弹出的上下文菜单中挑选“生成”按钮,以下图所示:
9、至此,Windows效劳已建立终了。
三、建立装置、启动、住手、卸载效劳的Windows窗体
1、在同一个解决方案里新建一个Windows Form项目,并命名为WindowsServiceClient,以下图所示:
2、将该项目设置为启动项目,并在窗体内增加四个按钮,分别为装置效劳、启动效劳、住手效劳及卸载效劳,以下图所示:
3、按下F7进入代码编辑界面,援用“System.ServiceProcess”及“System.Configuration.Install”,并输入以下代码:
using System; using System.Collections; using System.Windows.Forms; using System.ServiceProcess; using System.Configuration.Install; namespace WindowsServiceClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string serviceFilePath = $"{Application.StartupPath}\\MyWindowsService.exe"; string serviceName = "MyService"; //事宜:装置效劳 private void button1_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); } //事宜:启动效劳 private void button2_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName); } //事宜:住手效劳 private void button4_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName); } //事宜:卸载效劳 private void button3_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); } } //推断效劳是不是存在 private bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { return true; } } return false; } //装置效劳 private void InstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; IDictionary savedState = new Hashtable(); installer.Install(savedState); installer.Commit(savedState); } } //卸载效劳 private void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } //启动效劳 private void ServiceStart(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Stopped) { control.Start(); } } } //住手效劳 private void ServiceStop(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } } } }
4、为了后续调试效劳及装置卸载效劳的须要,将已生成的MyWindowsService.exe援用到本Windows窗体,以下图所示:
5、因为须要装置效劳,故须要运用UAC中Administrator的权限,鼠标右击项目“WindowsServiceClient”,在弹出的上下文菜单中挑选“增加”->“新建项”,在弹出的挑选窗体中挑选“应用顺序清单文件”并单击肯定,以下图所示:
6、翻开该文件,并将<requestedExecutionLevel level="asInvoker" uiAccess="false" />改成<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,以下图所示:
7、IDE启动后,将会弹出以下所示的窗体(有的体系因UAC设置有能够不显现),须要用管理员权限翻开:
8、从新翻开后,在IDE运转WindowsServiceClient项目;
9、运用WIN+R的体式格局翻开运转窗体,并在窗体内输入services.msc后翻开效劳,以下图所示:
10、点击窗体内的“装置效劳”按钮,将会在效劳中涌现MyService,以下图所示:
11、点击“运转效劳”按钮,将启动并运转效劳,以下所示:
12、点击“住手效劳”按钮,将会住手运转效劳,以下图所示:
13、点击“卸载效劳”按钮,将会从效劳中删除MyService效劳。
14、以上启动及住手效劳将会写入D:\MyServiceLog.txt,内容以下所示:
源代码下载:MyWindowsService_jb51.rar
补充:怎样调试效劳
1、要调试效劳,实在很简单,如需将效劳附加历程到须要调试的项目内里即可,假如要调试适才建的效劳,如今OnStop事宜里设置断点,以下所示:
2、启动“WindowsServiceClient”项目,在“调试”菜单中挑选“附件到历程”(效劳必需事前装置),以下所示:
3、找到“MyWindowsService.exe”,点击“附加”按钮,以下图所示:
4、点击“住手效劳”按钮,顺序将会在设置断点的处所中断,以下图所示:
以上就是Windows效劳在C#下怎样建立(图)?的细致内容,更多请关注ki4网别的相干文章!