zoukankan      html  css  js  c++  java
  • 创建一个Windows Service 程序

    1.新建Windows项目,选择"Windows服务"类型的项目。

    2.在生成的Service1.cs中代码中写你需要的代码,如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Timers;
    using System.IO;

    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();
            }
        }
    }

    3.在和Service1.cs这个项目下在新建一个安装程序类Installer1.cs,代码如下:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Linq;


    namespace WindowsService1
    {
        [RunInstaller(true)]
        public partial class Installer1 : Installer
        {
            private System.ServiceProcess.ServiceProcessInstaller spInstaller;
            private System.ServiceProcess.ServiceInstaller sInstaller;

            public Installer1()
            {
                InitializeComponent();

                // 创建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 = "MyTestWindowsService1";
                //设定服务启动的方式            
                this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
                this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });
            }
        }
    }

    4.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,对应的目录为:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe,然后执行InstallUtil.exe+待执行的exe文件的目录,如:InstallUtil.exe F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。

    打开Windows服务列表方式:运行 --> 输入services.msc 

    在列表中查找一个叫MyTestWindowsService1 的服务,这个就是你创建安装的服务

     

    5.启动该服务,这时打开bin\Debug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。

     

    6.卸载服务的操作也和简单,打开命令行工具,转到C:\Windows\Microsoft.NET\Framework\v4.0.30319目录,然后执行InstallUtil.exe -u F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe命令就可以了。

    专注iOS、Android、Java、Golang开发等涉及开发管理相关。 技术博客:http://xiaopin.cnblogs.com
  • 相关阅读:
    Atiitt 对象转换json 序列化规范 Java 循环引用的解决 设置序列化层次深度 去除不必的属性 太长不方便月度 jsonObject.remove("num1"); Prety fo
    Atitit 研发管理之道 attilax总结 艾龙 著 研发管理 1 简介 1 基本理念 2 基本原则 2 内容 3 团队建设 4 流程设计 4 成本管理 4 项目管理 4 绩效管理 4 风险管理
    Atitit 软件设计中的各种图纸 uml 之道 1. 常见设计成果与图纸 1 1.1. ui原型图与html 1 1.2. 业务逻辑 伪代码 各种uml图 1 1.3. 业务逻辑 流程图 ns
    Atitit ForkJoinTask的使用以及与futuretask的区别 1.1. Forkjoin原理图 1 1.2. Fork/Join使用两个类完成以上两件事情:ForkJoinTask
    Atitit 利用前端cache indexdb localStorage 缓存提升性能优化attilax总结 1.1. indexdb 更加强大点,但是结果测试,api比较繁琐 使用叫麻烦些 1
    Atitit pg10分区 总结 1.1. create table tmp_log (  1 1.2. 创建索引 1 1.3. 查看表 in pgadmin4 2 2. 二 分区表管理 2 2.1
    ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法(转载)
    .net3.5SP1开发项目引发的血案
    仿QQ弹出窗口
    ASP.NET中的数据绑定:哪个更快?
  • 原文地址:https://www.cnblogs.com/xiaopin/p/2242180.html
Copyright © 2011-2022 走看看