實時監控文件夾應用(WinForm 中通過結合windowsService一起使用)

要求:开机即可进行对于文件夹进行侦测是否有创建文件(方法:使用windowsService 和 fileSystemWatcher 监控文件夹)
1.从WindowsService 说起,新建一个windowService 文件,命名为:LogTransferService.cs
2.拖入一个FileSystemWatcher 命名为 fileSystemWatcher1 :侦听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。
3. 通过一个方法设置侦听的文件夹,对文件夹操作的方式,侦听文件的类型.........

//<summary>
         //使用FileSystemWatcher:偵聽文件系統創建通知,并在文件發生更改的時候引發事件。
         //</summary>
        private void UsingFileSystemWatcher()
        {
            string watcherFilePath = "E:\\POS_WS\\POS_LogTransferService\\POS_LogTransferService\\UpdateLog";
             //設置偵聽目錄
            fileSystemWatcher1.Path = watcherFilePath;
            fileSystemWatcher1.NotifyFilter =  NotifyFilters.FileName | NotifyFilters.LastAccess;
            fileSystemWatcher1.Filter = "*.txt";
            //是否監視子目錄
            fileSystemWatcher1.IncludeSubdirectories = false;

            #region 觸發的事件(文件创建的时候触发事件  4 )
            fileSystemWatcher1.Created +=new FileSystemEventHandler(fileSystemWatcher1_Created);
            #endregion

        }
4.     #region  觸發事件的方法
        /// <summary>
        /// 文件創建的時候觸發
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
        {
            TransferType transferType = new TransferType();
            //獲得后綴
            string suffix = e.Name.Substring(e.Name.LastIndexOf('.')+1, e.Name.Length - e.Name.LastIndexOf('.')-1);
            string fileName = fileSystemWatcher1.Path + "\\" + e.Name;
            if (suffix == "txt")
                transferType = TransferType.TextFileTransfer;
            Transfer(fileName, transferType);
        }
        #endregion
5.    在服务启动时候进行检测:

         /// <summary>
        /// 服務啟動
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
           
            //使用FileSystemWatcher:侦听文件系统
            UsingFileSystemWatcher();
            //服務開啟的時候,發送郵件通知
            SendMsgAboutServer(MsgSenderType.EmailSender, TransferService.ServerStart);
        }
6.    安装与卸载服务
在windowsService 页面  右键  加入安装程式自动生成 ServcieProcessInstall1 和ServiceInstaller 两个
通过ServiceProcessInstall 可以进行设置  访问的方式:Account:LocalService,NetWorkService,LoaclSystem,User
通过ServiceInstaller可以设置 DispalyName(安装服务后的显示名称),ServiceName,StartType(服务启动方式:自动或者手动)
7.生成解决方案:在该项目的debug 文件夹下面会有 生成服务的.exe 文件
安装该文件的命令:从Visual studio tools-->进入命令提示符--->进入生成的.exe路径 
安装:输入命令: instalutil  POS_LogTransferService.exe
卸载:输入命令:installutil /u POS_LogTransferService.exe

转载于:https://www.cnblogs.com/luqioudi/archive/2009/08/13/1545567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using opc_Library; namespace Files { /// /// Form1 的摘要说明。 /// public class Form1 : System.Windows.Forms.Form { write_log write_log = new write_log(); private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; private System.Windows.Forms.Button button1; private System.Windows.Forms.RichTextBox richTextBox1; private System.IO.FileSystemWatcher fileSystemWatcher1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Label label2; /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.button1 = new System.Windows.Forms.Button(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.fileSystemWatcher1 = new System.IO.FileSystemWatcher(); this.button2 = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(8, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(120, 16); this.label1.TabIndex = 0; this.label1.Text = "文件夹全路径名: "; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(8, 40); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(320, 21); this.textBox1.TabIndex = 1; this.textBox1.Text = "C:\\"; // // button1 // this.button1.Location = new System.Drawing.Point(232, 8); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(96, 23); this.button1.TabIndex = 2; this.button1.Text = "浏览文件夹"; this.button1.Click += new System.EventHandler(this.button1_Click_1); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(8, 88); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(320, 136); this.richTextBox1.TabIndex = 3; this.richTextBox1.Text = ""; // // fileSystemWatcher1 // this.fileSystemWatcher1.EnableRaisingEvents = true; this.fileSystemWatcher1.SynchronizingObject = this; this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Changed); this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Created); this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Deleted); this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed); // // button2 // this.button2.Location = new System.Drawing.Point(232, 64); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(96, 23); this.button2.TabIndex = 5; this.button2.Text = "开始监视"; this.button2.Click += new System.EventHandler(this.button2_Click); // // label2 // this.label2.Location = new System.Drawing.Point(8, 72); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(120, 16); this.label2.TabIndex = 4; this.label2.Text = "文件系统变化情况: "; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(336, 230);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值