C# windows 服务
来源:互联网 发布:域名url转发 编辑:程序博客网 时间:2024/06/10 12:07
一、如何用VS创建Windows服务
1、新建C# Windows服务:windows service工程
2、新建windows service工程后,系统自动生成一个Service1.cs文件,默认是其设计视图。选择查看其代码,默认有构造函数、OnStart、OnStop三个函数
3、新建了C# Windows服务之后,还要设置该服务运行的周期,左侧的ToolBox中有两个timmer,一个在组件下,一个在windows form下,可惜这两个都不能用,我们要手工新建一个timmer,并设置其属性和事件。
Service1.cs代码:
- public partial class Service1 : ServiceBase
- {
- public Service1()
- {
- InitializeComponent();
- System.Timers.Timer t = new System.Timers.Timer(1000);//实例化Timer类,设置间隔时间为10000毫秒;
- t.Elapsed += new System.Timers.ElapsedEventHandler(TimeElapse);//到达时间的时候执行事件;
- t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
- t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
- }
- public void TimeElapse(object source, System.Timers.ElapsedEventArgs e)
- {
- //EventLog log = new EventLog();
- //log.Source = "我的应用程序";
- //log.WriteEntry("1秒调用一次", EventLogEntryType.Information);
- FileStream fs = new FileStream(@"d:\timetick.txt", FileMode.OpenOrCreate, FileAccess.Write);
- StreamWriter m_streamWriter = new StreamWriter(fs);
- m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
- m_streamWriter.WriteLine("过了一秒 " + DateTime.Now.ToString() + "\n");
- m_streamWriter.Flush();
- m_streamWriter.Close();
- fs.Close();
- }
- protected override void OnStart(string[] args)
- {
- }
- protected override void OnStop()
- {
- }
- }
public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); System.Timers.Timer t = new System.Timers.Timer(1000);//实例化Timer类,设置间隔时间为10000毫秒; t.Elapsed += new System.Timers.ElapsedEventHandler(TimeElapse);//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; } public void TimeElapse(object source, System.Timers.ElapsedEventArgs e) { //EventLog log = new EventLog(); //log.Source = "我的应用程序"; //log.WriteEntry("1秒调用一次", EventLogEntryType.Information); FileStream fs = new FileStream(@"d:\timetick.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("过了一秒 " + DateTime.Now.ToString() + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); } protected override void OnStart(string[] args) { } protected override void OnStop() { } }4、服务编写之后,还不能由SCM(服务控制管理器)进行管理,需要给该服务添加装载器。在Service1.cs的设计视图,点击右键,选择“添加装载器”,系统默认就会添加ProjectInstaller.cs这个类。
5、添加该类后,在该类的设计视图上可看到serviceInstaller1和serviceProcessInstaller1,分别设置其属性。
设置serviceInstaller1的运行方式为手动或者自动
设置serviceInstaller1的ServiceName,设置为什么,服务列表中就显示什么
设置serviceProcessInstaller1的运行账号为LocalSystem
6、编译该工程
7、使用vs自带的命令行工具,运行installutil 编译生成的exe
8、在系统的服务中可看到我们创建的服务。
需要注意的是:
如果你修改了这个服务,路径没有变化的话是不需要重新注册服务的,如果路径发生了变化,需要先卸载这个服务InstallUtil.exe /u参数,然后再重新安装这个服务,不能直接安装。还有就是C# Windows服务是没有界面的,不要企图用控制的输出方式来输出一些信息,你只能添加一个EventLog,通过WriteEntry()来写日志。
二、安装与卸载
install.bat:
- @echo 安装WindowService
- @Set Path=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;
- @Set svn_dir=%cd%
- installutil %svn_dir%\WindowsService1.exe
- @echo 成功!
- pause
@echo 安装WindowService@Set Path=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;@Set svn_dir=%cd%installutil %svn_dir%\WindowsService1.exe @echo 成功!pause
uninstall.bat:
- @echo 卸载WindowService
- @Set Path=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;
- @Set svn_dir=%cd%
- installutil %svn_dir%\WindowsService1.exe /u
- @echo 成功!
- pause
@echo 卸载WindowService@Set Path=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;@Set svn_dir=%cd%installutil %svn_dir%\WindowsService1.exe /u @echo 成功!pause
解决“指定的服务已经标记为删除”问题:
该问题是运行删除服务项命令的时候,服务管理窗口未关闭引起的。
关闭服务管理窗口,重新删除、安装服务项即可。
其他:
net命令:
启动服务:net start Service1
停止服务:net stop Service2
sc命令:
启动服务:sc start Service1
停止服务:sc stop Service1
删除(卸载)服务:sc delete Service1
查看服务状态:sc query Service1
三、Local system/Network service/Local Service这三种账户区别
LocalSystem 账户
LocalSystem是预设的拥有本机所有权限的本地账户,这个账户跟通常的用户账户没有任何关联,也没有用户名和密码之类的凭证。这个服务账户可以打开注册表的HKEY_LOCAL_MACHINE\Security键,当LocalSystem访问网络资源时,它是作为计算机的域账户使用的。
举例来说,以LocalSystem账户运行的服务主要有:WindowsUpdate Client、 Clipbook、Com+、DHCP Client、Messenger Service、Task Scheduler、Server Service、Workstation Service,还有Windows Installer。
Network Service 账户
Network Service账户是预设的拥有本机部分权限的本地账户,它能够以计算机的名义访问网络资源。但是他没有Local System 那么多的权限,以这个账户运行的服务会根据实际环境把访问凭据提交给远程的计算机。Network Service账户通常可以访问Network Service、Everyone组,还有认证用户有权限访问的资源。
举例来说,以Network Service账户运行的服务主要有:Distributed Transaction Coordinator、DNS Client、Performance Logs and Alerts,还有RPC Locator。
Local Service 账户
Local Service账户是预设的拥有最小权限的本地账户,并在网络凭证中具有匿名的身份。Local Service账户通常可以访问Local Service、Everyone组还有认证用户有权限访问的资源。
举例来说,以Local Service账户运行的服务主要有:Alerter、Remote Registry、Smart Card、SSDP,还有WebClient。
- C#创建Windows服务
- C#创建Windows服务
- C#控制Windows服务
- c# 编写windows服务
- c# windows服务
- C# 创建 Windows 服务
- C# windows 服务
- c# windows服务程序
- C# 安装 Windows 服务
- C# 创建windows服务
- C# 监控Windows服务
- C# 写windows服务
- C#编写Windows服务
- C#写windows服务
- C#创建Windows服务
- c#写windows 服务
- C# 创建windows服务
- C# WINDOWS服务调试
- 1、redis入门第一篇
- Mysql 报错 1036 Table '表名' is read only
- Node后端数据渲染
- DefaultAnnotationHandlerMapping 和RequestMappingHandlerMapping
- Read_Vector
- C# windows 服务
- UnityUI之Dotween控制文本框移动
- Drupal8导入数据库时出错,需要全部忽略提示的问题
- vue 学习过程中遇到的一个非常气人的报错 Newline required at end of file but not found
- 利用多线程实现计时器Timer功能
- poj3262
- 通过一个小例子了解Java RMI
- git生成ssh并连接到github
- SpringMVC是什么?