FileSystemWatcher activated only once

醉酒当歌 提交于 2019-12-11 01:02:25

问题


I'm a beginner in C# and I'm stuck at this problem.

I'm creating a service, that watches for incoming files, and when the file comes, I will call a .bat file to do some processing.

The problem with the code below is, I copy the file into the watched folder for the first time it works, the second time I copy the file into the watched folder, it does not react anymore. It is running as a Windows service and it is running all the time.
It used to work, I'm not sure what I'm missing.

Hope someone can help me with this. Thank you.

namespace TestCSWinWatcherService
{
using System;
using System.IO;
using System.Configuration;


partial class SAPFileService
{
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.FSWatcherTest = new System.IO.FileSystemWatcher();
        ((System.ComponentModel.ISupportInitialize)(this.FSWatcherTest)).BeginInit();
        // 
        // FSWatcherTest
        // 
        this.FSWatcherTest.InternalBufferSize = 32768;
        this.FSWatcherTest.EnableRaisingEvents = true;
        this.FSWatcherTest.NotifyFilter = ((System.IO.NotifyFilters)((((((((System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName) 
        | System.IO.NotifyFilters.Attributes) 
        | System.IO.NotifyFilters.Size) 
        | System.IO.NotifyFilters.LastWrite) 
        | System.IO.NotifyFilters.LastAccess) 
        | System.IO.NotifyFilters.CreationTime) 
        | System.IO.NotifyFilters.Security)));
        this.FSWatcherTest.Changed += new System.IO.FileSystemEventHandler(this.FSWatcherTest_Changed);
        this.FSWatcherTest.Created += new System.IO.FileSystemEventHandler(this.FSWatcherTest_Created);
        this.FSWatcherTest.Deleted += new System.IO.FileSystemEventHandler(this.FSWatcherTest_Deleted);
        this.FSWatcherTest.Renamed += new System.IO.RenamedEventHandler(this.FSWatcherTest_Renamed);
        // 
        // SAPFileService
        // 
        this.ServiceName = "Service1";
        ((System.ComponentModel.ISupportInitialize)(this.FSWatcherTest)).EndInit();

    }

    #endregion

    private System.IO.FileSystemWatcher FSWatcherTest;

    /* DEFINE WATCHER EVENTS... */
    /// <summary>
    /// Event occurs when the contents of a File or Directory are changed
    /// </summary>
    private void FSWatcherTest_Changed(object sender,
                    System.IO.FileSystemEventArgs e)
    {
        //code here for newly changed file or directory

        String SAPInboundPath = ConfigurationManager.AppSettings["WatchPath"];
        String SAPArchivePath = ConfigurationManager.AppSettings["SAPArchivePath"];
        String SAPLogPath = ConfigurationManager.AppSettings["SAPLogPath"];
        String SAPDBCDFileName = ConfigurationManager.AppSettings["SAPDBCDFileName"];
        Boolean SAPDBCDExists = false;

        string[] files = System.IO.Directory.GetFiles(SAPInboundPath, SAPDBCDFileName, System.IO.SearchOption.TopDirectoryOnly);
        if (files.Length > 0)
        {
            //file exist
            SAPDBCDExists = true;
        }

        if (SAPDBCDExists)
        {
            String BatPath = ConfigurationManager.AppSettings["SAPBatPath"];
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(BatPath);
            psi.RedirectStandardOutput = true;
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            psi.UseShellExecute = false;
            System.Diagnostics.Process listFiles;
            listFiles = System.Diagnostics.Process.Start(psi);
            System.IO.StreamReader myOutput = listFiles.StandardOutput;
            listFiles.WaitForExit();
            //System.IO.File.Move(e.FullPath, SAPArchivePath + e.Name + DateTime.Now.ToString("dMMyyyyHHmmss"));
        }

    }

I've tried:

1) Your sleep hack

2) Increasing Buffer

3) Checking the GC does not clear

Nothing works... :(

来源:https://stackoverflow.com/questions/17131314/filesystemwatcher-activated-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!