创建一个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命令就可以了。
创建一个Windows Service 程序的更多相关文章
- C#创建一个Windows Service
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- 使用Windows Service Wrapper快速创建一个Windows Service
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
- 使用Windows Service Wrapper快速创建一个Windows Service 如nginx
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
- 用 vs2013 创建 windows service 程序
windows services 是运行在后台的服务程序,可以用 vs2013 来创建,创建的步骤如下: 1.打开 vs2013 , Files -->New Project --> wi ...
- C#Windows Service程序的创建安装与卸载
C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...
- 使用Visual Studio 2008创建你的第一个Windows Mobile程序介绍
使用Visual Studio 2008创建你的第一个Windows Mobile程序介绍 Windows MobileMobileWindowsMicrosoftWinForm 介绍 Microso ...
- 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 创 ...
- Step by Step 创建一个WCF Service
原创地址:http://www.cnblogs.com/jfzhu/p/4025448.html 转载请注明出处 (一)创建WCF Service (1)创建WCF Service类库 创建一个Cla ...
- 为MongoDB创建一个Windows服务
一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...
随机推荐
- 标准C程序设计七---05
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Scrapy学习-3-Request回调巧用
基于twisted的异步回调 使得页面爬取有阶段性和连续性 from scrapy.http import Request from urllib import parse def parse(sel ...
- android test控件
1.Plain Text 输入文本框 <EditText android:id="@+id/editText" android:layout_width="wrap ...
- Codeforces #471
C(分段) 题意: 分析: 我们分别考虑p=2和p>=3的情况 当p=2的时候,个数明显是[L,R]内完全平方数的个数 当p>=3的时候,我们注意到这样的数字个数是1e6级别的,且a最多也 ...
- PLsql/Oracle数据库中没有scott账户,如何创建并解锁
当然首先要装好Oracle 11g 然后还要有sqlplus,这个在Oracle11g的时候应该都会配上的 进入正题,如果oracle/plsql没scott账户,如何创建 先找到Oracle安装目录 ...
- 利用广播调用后台服务方法并根据方法返回的内容更新UI
一.UI布局代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...
- java消息队列怎么用
消息队列的使用场景是怎样的? 经常听到诸如rebbitmq,activemq,请教一下各位前辈消息队列的使用场景是怎样的,什么时候会用到它 校验用户名等信息,如果没问题会在数据库中添加一个用户记录 ...
- JAVA获取前一个月的第一天和最后一天
package com.date; import java.text.SimpleDateFormat; import java.util.Calendar; /** * 默认显示前一个月的第一天和最 ...
- lamp安装手稿
1.最重要的东西如何查看帮助 --help 文件夹简易意义:管理类文件夹/boot 启动文件/bin 常用命令/sbin 系统管理员的管理程序/var 存放常修改文件/etc 系统管理用到配置文件/d ...
- 【深度探索c++对象模型】关于对象
Linux进程的五个段 BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属 ...