-
C# 制作系统服务 实例源码下载
附件中有详细的安装使用文档,大概步骤如下:1.新建Windows项目,选择"Windows服务"类型的工程。2.生成的Program.cs文件中,定义了服务启动的Main函数。 代码 namespace WindowsService1{ static class Program { /// /// 应用程序的主入口点。 /// static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } }} 3.在新建的工程中,点击Service1.cs文件,切换到代码视图,生成的代码继承于ServiceBase基类,并重载了OnStart和OnStop方法。我在这个文件中进行了一些简单的操作,就是在服务开始的时候,定义一个定时器,然后每隔1秒钟,向文件中写入当前时间。 代码 namespace WindowsService1{ public partial class Service1 : ServiceBase { Timer timer; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { timer = new Timer(1000); timer.Elapsed = new ElapsedEventHandler(timer_Elapsed); timer.Start(); } protected override void OnStop() { timer.Stop(); timer.Dispose(); } void timer_Elapsed(object sender, ElapsedEventArgs e) { string filePath = AppDomain.CurrentDomain.BaseDirectory "test.txt"; StreamWriter sw = null; if (!File.Exists(filePath)) { sw = File.CreateText(filePath); } else { sw = File.AppendText(filePath); } sw.Write("访问时间:" DateTime.Now.ToString() Environment.NewLine); sw.Close(); } }}4.向工程中添加一个安装程序类。 4.在新添加的安装程序类中,设定服务的名称,启动方式,账号名和密码等信息。 代码 namespace WindowsService1{ partial class Installer1 { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; private System.ServiceProcess.ServiceProcessInstaller spInstaller; private System.ServiceProcess.ServiceInstaller sInstaller; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { components = new System.ComponentModel.Container(); // 创建ServiceProcessInstaller对象和ServiceInstaller对象 this.spInstaller =new System.ServiceProcess.ServiceProcessInstaller(); this.sInstaller = new System.ServiceProcess.ServiceInstaller(); // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息 this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.spInstaller.Password = null; this.spInstaller.Username = null; // 设定服务的名称 this.sInstaller.ServiceName = "WindowsService1"; //设定服务启动的方式 this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic; this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller,this.sInstaller}); } #endregion }}5.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,我安装的是VS 2010,对应的目录为:C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe,然后执行InstallUtil.exe 待执行的exe文件的目录,如:InstallUtil.exe F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。 6.启动该服务,这时打开binDebug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。7.停止服务的操作也和简单,打开命令行工具,转到C:WindowsMicrosoft.NETFrameworkv4.0.30319目录,然后执行InstallUtil.exe - u F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe命令就可以了。
- 2015-04-21下载
- 积分:1
-
wpf 自定义控件 实例源码下载(imagebutton)
wpf imagebutton
- 2015-01-30下载
- 积分:1
-
黑马24期c++基础课件资料
黑马24期c 基础课件资料
- 2020-12-04下载
- 积分:1
-
C# 和西门子S7200 Smart S7TCP 通信
① S7TCPDLL,此库支持西门子S7 TCP协议,支持S7-200smart,1200,300,400,1500 PLC等产品 ②此库由C#开发,支持VB.NET,C#.NET,VC.NET平台,在.NET平台之间引用即可,其他平台如vc ,vb请参考相关说明 ③利用此DLL完全可取代HMI (触摸屏),也可直接和PLC通讯,DEMO(VS2013,C#)见百度云,获取正式版请联系,或关注微信公众号
- 2019-12-13下载
- 积分:1
-
AutoCAD .net 二次开发源码 下载
用VS.NET2005开发ObjectARX程序 调试方法1.自动启动AutoCAD:选择工程根目录(解决方案下面的),鼠标右键-->属性-->工程属性对话框-->调试标签-->启动操作:启动外部程序-->浏览选择AutoCAD的安装目录,选择acad.exe。2.手动加载类库:(1) 按F5;(2) 自动启动AutoCAD,一路继续。(3) 在CAD命令行手动输入"netload",浏览到自己的动态连接库文件。3.自动加载类库:(1) 工程属性对话框-->调试标签-->启动选项-->命令行参数中输入:/nologo /b "....start.scr"让CAD自动在命令执行工程目录里的start.scr文件。(2) 然后就是在工程目录的根目录创建一个文本文件,名字取为“start.scr”,并在此文件中输入如下文本:netload "....indebuglubanren_2008.dll" (3) 按F5。4.单纯的数据库交互窗体操作,使用Windows 应用程序开发模式调试会更方便些:(1) 工程属性对话框-->应用程序标签-->程序集名称:Windows 应用程序。(2) 调试标签-->启动操作:启动项目。(3) 按F5。
- 2013-08-30下载
- 积分:1
-
asp.net 使用统计图表功能源码-饼图
推荐几个 不错的图表开源js ichartjs:http://www.ichartjs.com/ nvd3:http://nvd3.org/ highcharts:http://www.highcharts.com/ echarts:http://ecomfe.github.io/echarts/index.html
- 2014-01-25下载
- 积分:1
-
mvc4 PetsStore微软实例源码下载
mvc4 PetsStore微软实例源码下载
- 2014-07-25下载
- 积分:1
-
C# FTP上传文件、下载文件 实例源码下载
C# FTP上传文件、下载文件 实例源码下载
- 2017-06-24下载
- 积分:1
-
easyui 工作流设计器设计 例子源码
easyui 工作流设计器设计 例子源码
- 2014-11-04下载
- 积分:1
-
C# 短信猫 发短信例子源码
C# 短信猫 发短信例子源码
- 2015-07-01下载
- 积分:1