• 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);
                }
            }
        }
    }
  • 相关阅读:
    error: <item> inner element must either be a resource reference or empty.
    PEM routines:PEM_read_bio:no start line
    Android Https双向认证 + GRPC
    git tag用法
    Linux文件查找
    Detected problems with API compatibility(visit g.co/dev/appcompat for more info)
    Android NDK开发调试
    Beyond-Compare 4 -linux 破解
    Ubuntu下Gradle环境配置
    多线程系列一
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1647898.html
Copyright © 2020-2023  润新知