创建Windows Service
|
基本参照使用C#创建Windows服务,添加了部分内容 目录创建Windows Service 创建Windows Service选择C#标签的Windows Service项目,并创建
初始结构目录如下
修改Service1为TimingService 双击TimingService.cs,如图所示
点击末尾的 switch to code view进入代码编辑页面
public partial class TimingService : ServiceBase
{
public TimingService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
在OnStart和OnStop方法中写上服务启动、停止需要完成的工作
出现两个组件, serviceProcessInstaller1和serviceInstaller1,对它们的属性进行修改
serviceInstaller1
ServiceName改为TimingService,是在windows service列表里面的服务名称 Description改为"一个计时器",这里设置的是在windows service列表里面的服务描述 StartType默认为Manual(手动) 相关资料: ServiceInstaller Class serviceProcessInstaller1
将Account改为 LocalSystem右键项目点击 生成,在binDebug目录下生成MyWindowsService.exe至此,Windows Service创建完毕 相关资料: ServiceProcessInstaller Class 可视化管理Windows Service创建一个Windows Form项目
在窗体内添加四个按钮,分别为安装服务、启动服务、停止服务及卸载服务
引入System.ServiceProcess.dll和System.Configuration.Install.dll,完善相关功能 //需要引用MyWindowsService项目
string serviceFilePath = $"{Application.StartupPath}MyWindowsService.exe";
//这里是设置的serviceName,不是项目名称或者生成的exe的名称
string serviceName = "TimingService";
/// <summary>
/// 安装服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InstallButton_Click(object sender,EventArgs e)
{
if (this.IsServiceExisted(serviceName))
this.UninstallService(serviceName);
this.InstallService(serviceFilePath);
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartButton_Click(object sender,EventArgs e)
{
if (this.IsServiceExisted(serviceName))
this.ServiceStart(serviceName);
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StopButton_Click(object sender,EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UninstallButton_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();
}
}
}
为了后续管理服务方便,在MyWindowsService.Client引用MyWindowsService 获得管理员权限需要使用Administrator的权限才能安装服务
将原来的内容 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> 改为下面的内容 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 以管理员身份启动程序的相关资料:
可以右键服务点击 属性进行管理
调试很多种方式, 示例代码示例代码 参考资料使用C#创建Windows服务 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows-server-2008 – 映射驱动器错误 – 系统错误1808
- windows – RAD工作室需要很长时间才能打开
- 我需要仅使用本机Windows命令在批处理环境变量中匹配或替换
- windows-store-apps – 集成bing map的windows store app在
- .net – 无法使用EF5和VS 2010加载文件或程序集“Microsoft
- Windows Vista 64位的命令行subversion客户端
- windows – 使用ShellExecuteEx启动外部应用程序并等待它初
- windows-server-2008 – 将Windows Server 2008(不是R2)的就
- 在Windows 7上安装IIS8?
- windows-server-2008 – 远程桌面服务授权 – 服务器是否必
- .net – 我将如何执行WPF Windows应用程序的许可
- Windows Server 2003服务器C盘空间不足的N个解决
- Windows 2008服务器r2上的mysqltuner.pl
- windows-server-2008 – Windows x64上的MemCach
- 如何在不看到Microsoft标头的情况下运行WSH脚本?
- windows-server-2008-r2 – windws server 2008
- ms-access – 从Microsoft Access计算字符串的MD
- 远程桌面,身份验证错误:要求的函数不正确等解决
- Windows Phone 7 – 可以在wp7应用中播放Youtube
- windows – x86逆转挑战中的打包和加密部分,没有












