虚拟需求:编写一个Window服务,并注册到操作系统的服务里。让他隔30秒运行一下(写当前日期到一个文本里)

步骤:

  1. 创建一个Window 窗体应用程序项目(Greatwall.Mes.WindowsService)
  2. 添加一个新项,类型为Window 服务(TestService.cs)
  3. using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers; namespace Greatwall.Mes.WindowsService
    {
    partial class TestService : ServiceBase
    {
    public TestService()
    {
    InitializeComponent();
    } Timer timerTest;
    protected override void OnStart(string[] args)
    {
    // TODO: 在此处添加代码以启动服务。 timerTest = new Timer();
    timerTest.Interval = 30000;
    timerTest.Elapsed += timerTest_Elapsed;
    timerTest.Enabled = true; } void timerTest_Elapsed(object sender, ElapsedEventArgs e)
    {
    SaveLog();
    } protected override void OnStop()
    {
    // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
    timerTest.Enabled = false; } private void SaveLog()
    {
    string logPath = "C:\\Log.txt";
    string strLog = DateTime.Now.ToString() + "\n\r";
    if (!File.Exists(logPath))
    {
    FileStream fs1 = new FileStream(logPath, FileMode.Create, FileAccess.Write);//创建写入文件
    StreamWriter sw = new StreamWriter(fs1); sw.WriteLine(strLog);
    sw.Close();
    fs1.Close();
    }
    else
    {
    StreamWriter sw = File.AppendText(logPath);
    sw.Write(strLog);
    sw.Close();
    } }
    }
    }

      

  4. 点中TestService.cs(视图模式),右键---添加安装程序,系统会自动生成一个ProjectInstaller.cs文件
  5. 设置erviceProcessInstaller1的Account=LocalService
  6. 设置serviceInstaller1的StartType=Automatic
  7. 删除Form1那个创建时自动生成的文件
  8. 修改Program.cs文件
  9.  static void Main()
    {
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[]
    {
    new TestService()
    };
    ServiceBase.Run(ServicesToRun);
    }

      

  10. 编译成功后,代码部分就算结束了,开始注册服务到服务器上了。
  11. 将Bing下的Debug文件夹拷贝到D盘根目录上
  12. 打开Dos命令执行窗口
  13. 安装
  14. cd "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"

    InstallUtil.exe D:\Debug\Greatwall.MOM.R1.WindowsServcie.exe

  15. 看提示,如提示成功就到管理工具--服务里看看TestService是否有了,如有了,手动启动一下。
  16. 再到C盘看看log.txt文件是否开始写日期了
  17. 卸载 InstallUtil.exe /u D:\Debug\Greatwall.MOM.R1.WindowsServcie.exe  [我本机卸载完后服务显示为禁用状态,再装就说已存在,重启后不见了]
  18. 问题1:启动服务时报错,后面是发现我没有去改Program.cs里的代码。
  19. 问题2:启动服务正常,但没开始写log.txt文件,后面发现是C盘权限的问题。手工创建一个并设置权限为EveryOne后正常。
  20. 代码Debug: 解决问题二时需要用到Debug,在解决方案中,附加进程(用户进程),进程就是Greatwall.MOM.R1.WindowsServcie.exe这个可执行文件,在代码中设置好断点就可以了。
  21. 修改配置文件:Greatwall.MOM.R1.WindowsServcie.exe.config 直接编辑。修改后重启服务即可

如何编写Window服务程序(C# )的更多相关文章

  1. 编写 Window 服务程序

    编写 Window 服务程序     一.直观认识Windows服务.        打开Windows“控制面板/管理工具/服务”,系统显示Windows服务列表.                  ...

  2. C# 编写Window服务基础(一)

    一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...

  3. 用 C 语言编写 Windows 服务程序的五个步骤

    Windows 服务被设计用于需要在后台运行的应用程序以及实现没有用户交互的任务.为了学习这种控制台应用程序的基础知识,C(不是C++)是最佳选择.本文将建立并实现一个简单的服务程序,其功能是查询系统 ...

  4. C语言编写Windows服务程序

    原文:C语言编写Windows服务程序 #include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000 // 间 ...

  5. 用C语言编写Windows服务程序的五个步骤

    Windows 服务被设计用于需要在后台运行的应用程序以及实现没有用户交互的任务.为了学习这种控制台应用程序的基础知识,C(不是C++)是最佳选择.本文将建立并实现一个简单的服务程序,其功能是查询系统 ...

  6. 编写windows服务程序

    2012-11-02 08:54 (分类:计算机程序) windows服务是一个运行在后台并实现勿需用户交互的任务的控制台程序,对于隐藏程序有很大帮助. 用了几天时间概括了编写windows服务程序的 ...

  7. C#编写window服务,一步一步(1)

    Window服务是啥,这里就不废话了,如何用在哪里用也不废话了,这里我这篇文章只是详述了我在vs2012中创建window服务的经过,希望对你有所帮助. 另外:我在编写服务过程中参考了 Profess ...

  8. C#编写Windows服务程序图文教程

    安装服务程序C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe  要安装的服务程序路径(如F:\***.exe)卸载服务程序C: ...

  9. C#编写Windows服务程序 (服务端),client使用 消息队列 实现淘宝 订单全链路效果

    需求: 针对 淘宝提出的 订单全链路 产品接入 .http://open.taobao.com/doc/detail.htm?id=102423&qq-pf-to=pcqq.group oms ...

随机推荐

  1. [LeetCode&Python] Problem 844. Backspace String Compare

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  2. freeswitch黑名单mod_blacklist使用

    freeswitch自带黑名单模块"mod_blacklist",此文只是对该模块简单使用的实例. 最近接到客户投诉有大量骚扰电话,而从源头查不太容易,因此想到的笨方法是将投诉人加 ...

  3. angular6新建项目

    mkdir  angular6project cd angular6project ng new demo      新建一个普通项目 ng new demo --routing  新建一个带路由的项 ...

  4. kafka-producer partitioner.class的使用

    partitioner.class的说明   在API客户端中封装好的partition( )方法会为消息选择一个分区编号.为了保证消息负载均衡到每个分区,可以通过使用默认方式或者 手动配置这个参数的 ...

  5. easyUI默认图标的使用

    使用格式如下: <table id="table" class="easyui-datagrid" style="width:600px;hei ...

  6. 使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development、production)

    使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development.production) --mode 指定环境模式 ...

  7. Updates were rejected because the tip of your current branch is behind 问题出现解决方案

    提供如下几种方式: 1.使用强制push的方法(多人协作时不可取): git push -u origin master -f 2.push前先将远程repository修改pull下来 git pu ...

  8. 黄聪:微信URL Scheme,URL唤起微信

    微信URL Scheme 在外部浏览器中,可以通过<a href="weixin://">打开微信APP 也可以通过加一些参数,打开微信APP里的指定页面 <a ...

  9. 解决双击excel文件打开多个excel.exe进程的问题

    解决双击excel文件打开多个excel.exe进程的问题有些时候,双击两个excel文件,会打开多个excel进程,不同进程之间不能复制粘贴公式,只能粘贴数值,很不方便.怎么样双击多个excel文件 ...

  10. [UE4]Circular Throbber,圆形的、环形的动态图标

    一.就是圆形的,转动的很多小圆点. 二.Circular Throbber.Appearance.Number Of Pieces:可以通知显示的小圆点个数. 三.Circular Throbber. ...