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命令就可以了。

创建一个Windows Service 程序的更多相关文章

  1. C#创建一个Windows Service

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  2. 使用Windows Service Wrapper快速创建一个Windows Service

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

  3. 使用Windows Service Wrapper快速创建一个Windows Service 如nginx

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

  4. 用 vs2013 创建 windows service 程序

    windows services 是运行在后台的服务程序,可以用 vs2013 来创建,创建的步骤如下: 1.打开 vs2013 , Files -->New Project --> wi ...

  5. C#Windows Service程序的创建安装与卸载

    C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...

  6. 使用Visual Studio 2008创建你的第一个Windows Mobile程序介绍

    使用Visual Studio 2008创建你的第一个Windows Mobile程序介绍 Windows MobileMobileWindowsMicrosoftWinForm 介绍 Microso ...

  7. 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】

    Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...

  8. Step by Step 创建一个WCF Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4025448.html 转载请注明出处 (一)创建WCF Service (1)创建WCF Service类库 创建一个Cla ...

  9. 为MongoDB创建一个Windows服务

    一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...

随机推荐

  1. dedecms--自定义session存值取值

    最近在用用dedecms开发项目,开发项目中遇到需要通过session存储信息在其他页面调取使用,但是对dedecms里面自带的session存储使用不好,我需要存储的是用户登录的时候信息,于是我就使 ...

  2. itext A4纸张横向创建PDF

    import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException; import com.lowagie ...

  3. 在C#中使用Json.Net进行序列化和反序列化及定制化

    序列化(Serialize)是将对象转换成字节流,并将其用于存储或传输的过程,主要用途是保存对象的状态,以便在需要时重新创建该对象:反序列化(Deserialize)则是将上面的字节流转换为相应对象的 ...

  4. CodeForces - 813C The Tag Game (树的dfs遍历)

    [传送门]http://codeforces.com/problemset/problem/813/C [题目大意]两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走.给你一棵树,想要从某个结 ...

  5. Effective Java P2 Creating and Destroying Objects

    This chapter concerns creating and destorying objects : when and how to create them, when and how to ...

  6. esrichina

    http://www.esrichina.com.cn/arcgis10.5/index.html#fillback=0100307b617b7b7b363736363039323733627b617 ...

  7. uibutton去掉点击后背景有阴影的方法

    1,将normal和highlight两种方式都设置上图片即可 UIButton *goback = [[UIButton alloc]initWithFrame:CGRectMake(5.0f, 5 ...

  8. ios 使用keychain来存储token

    注意事项: 1.>On iPhone, Keychain rights depend on the provisioning profile used to sign your applicat ...

  9. 23. 客户默认选项(Default Customer Options)

    Editing Email Templates Email Sender Contact Us

  10. hdoj-1856-More is better【并查集】

    More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) To ...