zoukankan      html  css  js  c++  java
  • c#编写Windos服务,以及部署安装过程。

    源码:/Files/csharponworking/FileMonitorService.zip
    主要代码:

    代码
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.IO;
    using System.Runtime.InteropServices;

    namespace FileMonitorService
    {
        
    public class Service1 : System.ServiceProcess.ServiceBase
        {
            
    /// <summary> 
            
    /// 必需的设计器变量。
            
    /// </summary>
            private System.ComponentModel.Container components = null;
            
    private bool servicePaused;
            
    private System.Diagnostics.PerformanceCounter fileChangeCounter;
            
    private System.Diagnostics.PerformanceCounter fileDeleteCounter;
            
    private System.Diagnostics.PerformanceCounter fileRenameCounter;
            
    private System.Diagnostics.PerformanceCounter fileCreateCounter;

            
    public Service1()
            {
                
    // 该调用是 Windows.Forms 组件设计器所必需的。
                InitializeComponent();

                
    // TODO: 在 InitComponent 调用后添加任何初始化
            }

            
    // 进程的主入口点
            static void Main()
            {
                System.ServiceProcess.ServiceBase[] ServicesToRun;
        
                
    // 同一进程中可以运行多个用户服务。若要将
                
    // 另一个服务添加到此进程,请更改下一行
                
    // 以创建另一个服务对象。例如,
                
    //
                
    //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
                
    //
                ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

                System.ServiceProcess.ServiceBase.Run(ServicesToRun);
            }

            
    /// <summary> 
            
    /// 设计器支持所需的方法 - 不要使用代码编辑器 
            
    /// 修改此方法的内容。
            
    /// </summary>
            private void InitializeComponent()
            {
                
    this.components = new System.ComponentModel.Container();
                
    this.fileChangeCounter = new System.Diagnostics.PerformanceCounter();
                
    this.fileDeleteCounter = new System.Diagnostics.PerformanceCounter();
                
    this.fileRenameCounter = new System.Diagnostics.PerformanceCounter();
                
    this.fileCreateCounter = new System.Diagnostics.PerformanceCounter();

                fileChangeCounter.CategoryName 
    = "File Monitor Service";
                fileDeleteCounter.CategoryName 
    = "File Monitor Service";
                fileRenameCounter.CategoryName 
    = "File Monitor Service";
                fileCreateCounter.CategoryName 
    = "File Monitor Service";

                fileChangeCounter.CounterName 
    = "Files Changed";
                fileDeleteCounter.CounterName 
    = "Files Deleted";
                fileRenameCounter.CounterName 
    = "Files Renamed";
                fileCreateCounter.CounterName 
    = "Files Created";

                
    this.ServiceName = "FileMonitorService";
                
    this.CanPauseAndContinue = true;
                
    this.CanStop = true;
                servicePaused 
    = false;
            }

            
    /// <summary>
            
    /// 清理所有正在使用的资源。
            
    /// </summary>
            protected override void Dispose( bool disposing )
            {
                
    if( disposing )
                {
                    
    if (components != null
                    {
                        components.Dispose();
                    }
                }
                
    base.Dispose( disposing );
            }

            
    /// <summary>
            
    /// 设置具体的操作,以便服务可以执行它的工作。
            
    /// </summary>
            protected override void OnStart(string[] args)
            {    
                FileSystemWatcher curWatcher 
    = new FileSystemWatcher();

                curWatcher.BeginInit();
                curWatcher.IncludeSubdirectories 
    = true;
                curWatcher.Path 
    = System.Configuration.ConfigurationSettings.AppSettings["FileMonitorDirectory"];
                curWatcher.Changed 
    += new FileSystemEventHandler(OnFileChanged);
                curWatcher.Created 
    += new FileSystemEventHandler(OnFileCreated);
                curWatcher.Deleted 
    += new FileSystemEventHandler(OnFileDeleted);
                curWatcher.Renamed 
    += new RenamedEventHandler(OnFileRenamed);
                curWatcher.EnableRaisingEvents 
    = true;
                curWatcher.EndInit();
            }

            
    /// <summary>
            
    /// 停止此服务。
            
    /// </summary>
            protected override void OnStop()
            {
                
    if( fileChangeCounter.RawValue != 0 )
                {
                    fileChangeCounter.IncrementBy(
    -fileChangeCounter.RawValue);
                }
                
    if( fileDeleteCounter.RawValue != 0 )
                {
                    fileDeleteCounter.IncrementBy(
    -fileDeleteCounter.RawValue);
                }
                
    if( fileRenameCounter.RawValue != 0 )
                {
                    fileRenameCounter.IncrementBy(
    -fileRenameCounter.RawValue);    
                }
                
    if( fileCreateCounter.RawValue != 0 )
                {
                    fileCreateCounter.IncrementBy(
    -fileCreateCounter.RawValue);
                }
            }

            
    /// <summary>
            
    /// 暂停服务
            
    /// </summary>
            protected override void OnPause()
            {
                servicePaused 
    = true;
            }

            
    /// <summary>
            
    /// 恢复服务
            
    /// </summary>
            protected override void OnContinue()
            {
                servicePaused 
    = false;
            }

            
    private void OnFileChanged(Object source, FileSystemEventArgs e)
            {
                
    if( servicePaused == false )
                {
                    fileChangeCounter.IncrementBy(
    1);
                }
            }
        
            
    private void OnFileRenamed(Object source, RenamedEventArgs e)
            {
                
    if( servicePaused == false )
                {
                    fileRenameCounter.IncrementBy(
    1);
                }
            }

            
    private void OnFileCreated(Object source, FileSystemEventArgs e)
            {
                
    if( servicePaused == false )
                {
                    fileCreateCounter.IncrementBy(
    1);
                }
            }

            
    private void OnFileDeleted(Object source, FileSystemEventArgs e)
            {
                
    if( servicePaused == false )
                {
                    fileDeleteCounter.IncrementBy(
    1);
                }
            }
        }
    }
  • 相关阅读:
    团队作业6——复审与事后分析
    Alpha阶段项目复审
    测试与发布( Alpha版本 )
    测试与发布(Alpha版本)
    团队作业4——项目冲刺
    第 1 篇 Scrum 冲刺博客
    第 7篇 Scrum 冲刺博客
    第 6篇 Scrum 冲刺博客
    第 5篇 Scrum 冲刺博客
    第 4篇 Scrum 冲刺博客
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1647898.html
Copyright © 2011-2022 走看看