2

我是 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不清除

没有任何效果... :(

4

0 回答 0