zoukankan      html  css  js  c++  java
  • 如何编写Window服务程序(C# )

    虚拟需求:编写一个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() + "
      
      ";
                  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:WindowsMicrosoft.NETFramework64v4.0.30319"

      InstallUtil.exe D:DebugGreatwall.MOM.R1.WindowsServcie.exe

    15. 看提示,如提示成功就到管理工具--服务里看看TestService是否有了,如有了,手动启动一下。
    16. 再到C盘看看log.txt文件是否开始写日期了
    17. 卸载 InstallUtil.exe /u D:DebugGreatwall.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 直接编辑。修改后重启服务即可
  • 相关阅读:
    APK自我保护方法
    Andorid APK反逆向解决方案---梆梆加固原理探寻
    判断android文件是否加壳
    java调用dll-JNA
    Java调用本地接口
    pat00-自测2. 素数对猜想 (20)
    pat00-自测4. Have Fun with Numbers (20)
    pat00-自测3. 数组元素循环右移问题 (20)
    pat00-自测1. 打印沙漏(20)
    pat1013. Battle Over Cities (25)
  • 原文地址:https://www.cnblogs.com/sportdog/p/8422134.html
Copyright © 2011-2022 走看看