c# 服务创建

服务

创建服务

编写服务

可以对server1.cs重新命名,点击你的server按F7进入代码编辑模式,编写脚本

双击你的server.cs右击空白位置,添加安装程序,此时会生成"serviceInstaller1"及"serviceProcessInstaller1"

后续可以点击ProjectInstaller.cs查看已经添加的程序

修改属性

右击serviceInstaller1属性修改servicename,用于等下启动服务的名称

右击serviceProcessInstaller1属性修改Account改为LocalSystem(服务属性系统级别)

至此服务已经编写完毕

启动服务

启动服务可以使用多种方式,这里介绍两种思路

创建bat文件启动

新建一个txt,等下修改为.bat,可以创建多个.bat用于安装服务,开启服务等

使用sc创建

//创建-配置-开启

@echo.服务启动......

@echo off

@sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32srvdemo\win32srvdemo\Debug\win32srvdemo.exe"

@net start 服务名

@sc config 服务名 start= AUTO

@echo off

@echo.启动完毕!

@pause

//关闭服务

@echo.服务关闭

@echo off

@net stop 服务名

@echo off

@echo.关闭结束!

@pause

//删除服务

@echo.服务删除

@echo off

@sc delete 服务名

@echo off

@echo.删除结束!

@pause

使用net framework提供的命令启动

//安装-启动-设置自动运行 - 暂停

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe "你的地址\MyTask.exe"

Net Start 服务名

sc config 服务名 start= auto

pause
//卸载服务

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u "你的地址\MyTask.exe"

pause

使用winform窗体,代码启动

点击解决方案,创建一个winform项目net framework

设置安装,卸载,启动和关闭按钮,设置单击事件

private void button1_Click(object sender, EventArgs e)

{

//安装服务

if (ServiceHelperOne.IsServiceExisted(serviceName))

ServiceHelperOne.UninstallService(serviceFilePath);

ServiceHelperOne.InstallService(serviceFilePath);

}

private void button_start_Click(object sender, EventArgs e)

{

//运行服务

if (ServiceHelperOne.IsServiceExisted(serviceName))

ServiceHelperOne.ServiceStart(serviceName);

}

private void button_stop_Click(object sender, EventArgs e)

{

//服务停止

if (ServiceHelperOne.IsServiceExisted(serviceName))

ServiceHelperOne.ServiceStop(serviceName);

}

private void button_unservice_Click(object sender, EventArgs e)

{

//服务卸载

if (ServiceHelperOne.IsServiceExisted(serviceName))

{

ServiceHelperOne.ServiceStop(serviceName);

ServiceHelperOne.UninstallService(serviceFilePath);

}

}

增加一个帮助类

public class ServiceHelperOne

{

//判断服务是否存在

public static bool IsServiceExisted(string serviceName)

{

ServiceController[] services = ServiceController.GetServices();

foreach (ServiceController sc in services)

{

if (sc.ServiceName.ToLower() == serviceName.ToLower())

{

return true;

}

}

return false;

}

//安装服务

public static void InstallService(string serviceFilePath)

{

try

{

using (AssemblyInstaller installer = new AssemblyInstaller())

{

installer.UseNewContext = true;

installer.Path = serviceFilePath;

IDictionary savedState = new Hashtable();

installer.Install(savedState);

installer.Commit(savedState);

}

}

catch (Exception ex)

{

var err = ex.Message;

}

}

//卸载服务

public static void UninstallService(string serviceFilePath)

{

using (AssemblyInstaller installer = new AssemblyInstaller())

{

installer.UseNewContext = true;

installer.Path = serviceFilePath;

installer.Uninstall(null);

}

}

//启动服务

public static void ServiceStart(string serviceName)

{

using (ServiceController control = new ServiceController(serviceName))

{

if (control.Status == ServiceControllerStatus.Stopped)

{

control.Start();

}

}

}

//停止服务

public static void ServiceStop(string serviceName)

{

using (ServiceController control = new ServiceController(serviceName))

{

if (control.Status == ServiceControllerStatus.Running)

{

control.Stop();

}

}

}

}

添加winform项目对服务的引用,添加exe文件

右击winform项目添加新项目->应用程序清单文件

打开该文件,并将<requestedExecutionLevel level="asInvoker" uiAccess="false" />改为<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

启动后,将会弹出如下所示的窗体(有的系统因UAC配置有可能不显示),需要用管理员权限打开

可以打开任务管理器,运行项目,安装运行可以查看服务的状态

调试服务

调试服务,需要将服务进程添加到需要调试的程序当中,可以设置在创建的服务项目里,在OnStop设置断点

点击调试-附加到进程

一般名称和执行exe名称一致,并不是设置的服务名

停止服务,查看断点,调试代码

相关推荐
d111111111d28 分钟前
在STM32中,中断服务函数的命名有什么要求?
笔记·stm32·单片机·嵌入式硬件·学习·c#
极客智造2 小时前
深入解析 C# Type 类:解锁反射与动态编程的核心
c#·反射
SmoothSailingT2 小时前
C#——textBox控件(1)
开发语言·c#
superman超哥2 小时前
仓颉语言中并发集合的实现深度剖析与高性能实践
开发语言·后端·python·c#·仓颉
工程师0073 小时前
C#中的服务注册剖析
c#·服务注册
张人玉3 小时前
c#DataTable类
数据库·c#
缺点内向3 小时前
如何在 C# .NET 中将 Markdown 转换为 PDF 和 Excel:完整指南
pdf·c#·.net·excel
CodeCraft Studio3 小时前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建旭日图
c#·excel·aspose·excel旭日图·excel库·excel开发控件·excel api库
民乐团扒谱机3 小时前
【微实验】仿AU音频编辑器开发实践:从零构建音频可视化工具
算法·c#·仿真·audio·fft·频谱
武藤一雄4 小时前
彻底吃透.NET中序列化反序列化
xml·微软·c#·json·.net·.netcore