/// 应用程序的主入口点。        ///         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命令就可以了。-IMDN开发者社群-imdn.cn">         /// 应用程序的主入口点。        ///         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命令就可以了。 - IMDN开发者社群-imdn.cn">
登录
首页 » C# » C# 制作系统服务 实例源码下载

C# 制作系统服务 实例源码下载

于 2015-04-21 发布
0 249
下载积分: 1 下载次数: 0

代码说明:

附件中有详细的安装使用文档,大概步骤如下: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命令就可以了。

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • 20多套IrisSkin2皮肤下载
    20多套IrisSkin2皮肤下载
    2020-12-09下载
    积分:1
  • C# 短信猫 发短信例子源码
    C# 短信猫 发短信例子源码
    2015-07-01下载
    积分:1
  • c# 断点续传 示例源码
    c# 断点续传 示例源码
    2014-03-22下载
    积分:1
  • 二分搜索算法
    实现二分查找的递归与非递归实现二分查找思想:1、数组从小到大排序;2、查找的key每次和中间数比较,如果key小于mid查找mid左侧的数组部分;如果key大于mid,则查找mid右侧的数组部分;如果相等,则直接返回mid。输入:排序数组-array,数组大小-aSize,查找值-key返回:返回数组中的相应位置,否则返回-1
    2021-05-06下载
    积分:1
  • vs2015 以太网继电器上位机配置源码
    vs2015 以太网继电器上位机配置源码
    2021-05-06下载
    积分:1
  • C# 设计模式观察者模式 入门级示例
    C# 设计模式观察者模式 入门级示例
    2018-11-19下载
    积分:1
  • C# 文件上传和下载(上传文件到服务端)
    实现文件上传和下载,包含服务器和客户端 对于学习TCP的同学有帮助
    2021-03-21 22:09:16下载
    积分:1
  • C# 删除、遍历指定目录下的所有指定文件、文件夹
    C# 删除、遍历指定目录下的所有指定文件、文件夹
    2013-10-31下载
    积分:1
  • C#程序设计经典案例设计与实现
    《Visual C# 2008程序设计经典案例设计与实现》源码下载 第1章 Visual C#2008与窗体界面案例1 飘动动画窗体案例2 透明动画窗体案例3 利用API函数实现动画窗体案例4 闪烁动画窗体案例5 滚动字幕动画窗体案例6 超女卡通窗体案例7 总在最前的登录窗体案例8 在屏幕中央的圓形窗体案例9 半透明的T形窗体案例10 多文档MDI窗体案例11 渐变色窗体案例12 笑脸窗体案例13 八边形图形窗体本章小结第2章 Visual C# 2008与图形图像案例1 动态绘制直线和曲线案例2 动态绘制验证码案例3 椭圓及椭圆弧的绘制案例4 移动鼠标复制坐标区域图像案例5 动态获取当前程序的图标案例6 动态获取系统图标案例7 动态打开、显示和缩放图像案例8 在图像上动态加载文字案例9 水平和垂直遮罩图像动画效果案例10 图像拉伸动画效果案例11 百叶窗图像动画效果案例12 翻转和扩展图像动画效果案例13 图像的纹理和浮雕效果案例14 图像的马赛克效果本章小结第3章 Visual C#2008与多媒体案例1 抛物线动画效果案例2 图像滚动动画效果案例3 飞舞的雪花案例4 电子时钟案例5 Flash动画播放器案例6 AvI动画播放器案例7 GⅣ动画播放器案例8 MP3音乐播放器案例9 肥皂泡泡屏幕保护程序案例1O 图像随机显示屏幕保护程序案例11 滚动字幕屏幕保护程序案例12 带有背景音乐的随机字幕屏幕保护程序案例13 托盘动画图标程序本章小结第4章 Visual C#2006与数据库案例1 利用DataGridView控件显示数据库信息案例2 数据库数据记录单案例3 利用下拉列表框动态查询数据库信息案例4 利用ListView控件导航数据库信息案例5 带有数据库的会员登录系统案例6 动态添加数据库数据信息案例7 动态修改数据库数据信息案例8 动态删除数据库数据信息案例9 带有图像信息的数据库数据记录单案例10 分页显示数据库中的记录数据信息案例11 连接Excel 2008表格案例12 连接SQL Server数据库本章小结第5章 Visual C#2008的SQL查询与图表技朮案例1 多表连接条件查询案例2 分组条件查询案例3 排序查询案例4 嵌套查询案例5 视图在Select查询语句中的应用案例6 存储过程在Select查询语句中的应用案例7 动态获取数据库中所有视图和存储过程案例8 柱状图表分析图案例9 柱状图表的升序和降序案例10 折线图表分析图案例11 多折线图表分析图案例12 饼形图表分析图本章小结第6章 Visualc#2008的打印与水晶报表技术案例1 利用Windows组件打印数据库中的数据案例2 利用Windows组件打印数据库数据柱状图表案例3 利用Windows组件打印输出图像案例4 调用Word软件打印数据库中的数据案例5 调用Excel软件打印数据库中的数据案例6 利用水晶报表打印数据库中的数据案例7 利用水晶报表分组统计数据库中的数据案例8 利用水晶报表筛选数据库中的数据案例9 图表在水晶报表中的应用本章小结第7章 VisualC#2008与Windows系统设置案例1 任务栏的显示与隐藏案例2 开始按钮的显示与隐藏案例3 动态设置系统输入法案例4 动态设置桌面颜色案例5 动态设置鼠标的属性案例6 动态获取鼠标位置案例7 声卡的检测及声音,音频设备属性的设置案例8 只能运行一次的托盘程序案例9 动态设置程序是否为开机运行案例10 动态设置注册表是否可运行案例11 禁止修改IE浏览器的主页案例12 动态锁定计算机案例13 计算机的注销、关闭和重新启动案例14 动态获取计算机系统基本信息本章小结第8章 VisualC#2008与文件案例1 动态创建文件并输入文件内容案例2 动态打开和保存文件案例3 动态删除文件及清空回收站案例4 动态创建和删除文件夹及显示其是否存在案例5 动态获取文件夹中的文件案例6 动态搜索文件或文件夹案例7 动态显示磁盘容量图表案例8 动态移动文件案例9 动态复制文件案例1O 动态调用可执行EXE文件案例11 动态查看和修改文件属性案例12 动态比较文件本章小结第9章 VisualC#2008算法及控件的应用案例1 利用冒泡算法实现从小到大排序案例2 利用希尔算法实现从大到小排序案例3 判断主机IP合法性算法案例4 欧几里德最大公因子算法案例5 字符串的加密算法案例6 随机生成新身份证算法案例7 利用ListBox控件实现数据源字段的选择案例8 利用LislNiew控件实现图标的管理案例9 利用TreeView控件浏览图像案例10 利用ProgressBar控件实现进度控制案例11 利用Timel控件显示当前时间本章小结第10章 VisualC#2008与网络开发案例1 动态获取本机MAC地址案例2 动态获取本机IP地址案例3 动态网络Ping操作案例4 定时访问互联网案例5 定制网络浏览器程序案例6 动态发送电子邮件案例7 动态读取XMI文件本章小结第11章 VisualC#2008与Web应用程序案例1 利用AdRotator控件随机显示广告图像案例2 利用Calendar控件动态查询系统日期案例3 会员登录系统案例4 利用Application对象实现网站计数器案例5 利用Session对象实现留言板本章小结
    2016-09-02下载
    积分:1
  • 模仿QQ播放器的简单音乐播放器
    可以从网易云音乐读取音乐,添加本地音乐等
    2016-05-11下载
    积分:1
  • 696516资源总数
  • 106914会员总数
  • 0今日下载