FileSystemWatcher只激活一次

时间:2013-06-16 08:04:26

标签: filesystemwatcher

我是C#的初学者,我遇到了这个问题。

我正在创建一个监视传入文件的服务,当文件到来时,我将调用.bat文件来进行一些处理。

以下代码的问题是,我第一次将文件复制到监视文件夹中,第二次将文件复制到监视文件夹中,它不再作出反应。它作为Windows服务运行,并且一直在运行 它曾经工作,我不知道我错过了什么。

希望有人可以帮助我。谢谢。

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"));
        }

    }

我试过了:

1)你的睡眠黑客

2)增加缓冲区

3)检查GC无法清除

没有任何作用...... :(

0 个答案:

没有答案