• 初识Windows服务 C#


    1.新建Windows服务


     

     

     

    2.切换到代码视图,拷入如下代码


    该服务以10S的间隔创建 d:/1.txt 文件

     

    1. 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 WindowsServiceTest
      {
      publicpartialclassService1:ServiceBase
      {
      publicService1()
      {
      InitializeComponent();
      }
      protectedoverridevoidOnStart(string[] args)
      {
      //服务开启执行代码
      StartDoSomething();
      }
      protectedoverridevoidOnStop()
      {
      //服务结束执行代码
      }
      protectedoverridevoidOnPause()
      {
      //服务暂停执行代码
      base.OnPause();
      }
      protectedoverridevoidOnContinue()
      {
      //服务恢复执行代码
      base.OnContinue();
      }
      protectedoverridevoidOnShutdown()
      {
      //系统即将关闭执行代码
      base.OnShutdown();
      }
      privatevoidStartDoSomething()
      {
      System.Timers.Timer timer =newSystem.Timers.Timer(10000);//间隔10秒
      timer.AutoReset=true;
      timer.Enabled=false;//执行一次
      timer.Elapsed+=newElapsedEventHandler(WriteSomething);
      timer.Start();
      }
      privatevoidWriteSomething(object source,System.Timers.ElapsedEventArgs e)
      {
      FileStream fs =null;
      try
      {
      fs =newFileStream("d:/1.txt",FileMode.OpenOrCreate);
      string strText =@"以10秒的间隔重复创建该文件,若已有同名文件,则保持不变";
      //获得字节数组
      byte[] data =new UTF8Encoding().GetBytes(strText);
      //开始写入
      fs.Write(data,0, data.Length);
      //清空缓冲区、关闭流
      fs.Flush();
      fs.Close();
      fs.Dispose();
      }
      catch
      {
      }
      finally
      {
      if(fs !=null)
      {
      fs.Close();
      fs.Dispose();
      }
      }
      }
      }
      }
      

        

    3.添加安装程序并设置控件属性


    1.在设计页面右键,选择添加安装程序

     

     


    2.将左上角第一个控件的Account属性设置为LocalService



    3.可以自行修改第二个控件的ServiceName(服务名称,不可和系统已有的冲突),StartType设置为Automatic


     

    4.编译项目


    1.生成解决方案(Ctrl+Shift+B),编译完成后会生成对应的xxx.exe

    2.找到系统里面InstallUtil的安装目录 例如 C:WindowsMicrosoft.NETFramework64v4.0.30319InstallUtil.exe  实在找不到就用Everything吧

    3.Win+R CMD  cd 跳转到InstallUtil的安装路径,运行如下命令  InstallUtil.exe+空格+4.1生成的exe的目录

        cd C:WindowsMicrosoft.NETFramework64v4.0.30319

        InstallUtil.exe E:GitVSTestWindowsServiceTestWindowsServiceTestinDebugWindowsServiceTest.exe


     

    5.启动服务


    在计算机-管理-服务和应用程序-服务 里面找到刚才编译的服务,右键启动即可

    6.修改服务


    1.在服务里面,停止对应服务
    2.修改源代码并再次生成解决方案(Ctrl+Shift+B)
    3.再次启动服务

    7.卸载服务


    卸载服务运行的命名和4.3 类似
    依然是在InstallUtil 目录下,不过是运行 InstallUtil.exe /u 生成的exe的目录
    例如:C:WindowsMicrosoft.NETFramework64v4.0.30319>InstallUtil.exe /u E:GitVSTestWindowsServiceT2WindowsServiceT2inDebugWindowsServiceT2.exe





    作者:Moonache
    出处:http://www.cnblogs.com/moonache/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 相关阅读:
    mysql5.7 ERROR 1045 (28000): Access denied for user解决方法
    C++ json解决方案
    Gitblit无法查看单个文件解决方案
    git 拉取指定的远程分支(三种方式)
    全栈12
    git commit之后,想撤销commit
    video
    iframe
    git恢复之前版本的两种方法reset、revert
    Eclipse
  • 原文地址:https://www.cnblogs.com/moonache/p/4644081.html
Copyright © 2020-2023  润新知