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名称一致,并不是设置的服务名

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

相关推荐
程序猿多布8 分钟前
XLua教程之热补丁技术
unity·c#·lua·xlua
咕白m6251 小时前
C# Excel 读取入门教程:免费实现方法
c#·.net
相与还2 小时前
godot+c#使用godot-sqlite连接数据库
数据库·c#·godot
相与还4 小时前
godot+c#操作sqlite并加解密
sqlite·c#·godot·sqlcipher
疯狂的维修4 小时前
关于Gateway configration studio软件配置网关
网络协议·c#·自动化·gateway
程序猿多布7 小时前
XLua教程之Lua调用C#
unity·c#·lua·xlua
唐青枫7 小时前
FluentData 从入门到精通:C#.NET 数据访问最佳实践
c#·.net
张晓~1833994812116 小时前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
almighty2718 小时前
C# DataGridView表头自定义设置全攻略
数据库·c#·winform·datagridview·自定义表头
arbboter20 小时前
【自动化】深入浅出UIAutomationClient:C#桌面自动化实战指南
运维·c#·自动化·inspect·uiautomation·uia·桌面自动化