sportdog

导航

 

虚拟需求:编写一个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 直接编辑。修改后重启服务即可

 

posted on 2018-02-06 14:13  sportdog  阅读(228)  评论(0编辑  收藏  举报