1

私は多くのフォームを検索しましたが、以下の問題の解決策が見つかりませんでした.誰かがこのフォーラムで私を助けることができることを願っています.

ネットワーク上のフォルダーを監視する2つのFSWを備えた.net Windowsサービスがあります。

フォルダ構成 \NetworkDrive\NewFolder\InputDirectory \NetworkDrive\NewFolder\WorkingDirectory

複数のファイルを \NetworkDrive\NewFolder\InputDirectory ディレクトリにコピーすると、たとえば、file1、file2、file3、および file4 の場合、file1、file2、および file3 のみが処理され、1 つのファイルが残ります。

以下はサービスクラスとファイルシステムウォッチャークラスのコードです

サービス クラス

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Security.Permissions;

namespace Service1
{
    public partial class Service1 : ServiceBase
    {
    public FaxInbound()
    {
        InitializeComponent();
        this.ServiceName = "Service1";
    }

    protected override void OnStart(string[] args)
    {
        onServiceStartProcess();
    }

    protected override void OnStop()
    {
        try
        {
        workingSysTimer.Enabled = false;
        inboundSysTimer.Enabled = false;
        workingSysTimer.Stop();
        inboundSysTimer.Stop();
        }
        catch (ApplicationException ex)
        {
        throw new ProblemException(ex.Message, ex.InnerException);
        }
    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    private void inboundSysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
        WatchFileSystem.Run(inboundFSW, inputDirectory, directoryWatchfilter);
        }
        catch (ApplicationException ex)
        {
        throw new ProblemException(ex.Message, ex.InnerException);
        }
        finally
        {
        inboundSysTimer.Enabled = true;
        inboundSysTimer.Start();
        }
    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    private void workingSysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
        WatchFileSystem.Run(workingFSW, workingDirectory, directoryWatchfilter);
        }
        catch (ApplicationException ex)
        {
        throw new ProblemException(ex.Message, ex.InnerException);
        }
        finally
        {
        workingSysTimer.Enabled = true;
        workingSysTimer.Start();
        }
    }

    internal void onServiceStartProcess()
    {
        try
        {
        setConfig();
        inboundSysTimer.Enabled = true;
        workingSysTimer.Enabled = true;
        inboundSysTimer.Start();
        workingSysTimer.Start();
        }
        catch (ApplicationException ex)
        {
        throw new ProblemException(ex.Message, ex.InnerException);
        }
    }
    }
}

FileSystemwatcher クラス

using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.Security.Permissions;

namespace Service1
{
    [Serializable()]
    internal class WatchFileSystem
    {
    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    internal static void Run(System.IO.FileSystemWatcher watcher, string directoryPath = "", string fileFilter = "*.*")
    {
        try
        {
        string args = directoryPath;

        if (args.Length < 3)
        {
            return;
        }

        watcher.Path = args;

        watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;

        watcher.Filter = fileFilter;
        watcher.InternalBufferSize = 64;

        if (watcher.Path == inputDirectory)
        {
            watcher.Changed += new FileSystemEventHandler(OnInputDirectoryChanged);
            watcher.Created += new FileSystemEventHandler(OnInputDirectoryChanged);
            watcher.Error += new ErrorEventHandler(OnInputDirectoryError);
        }

        if (watcher.Path == workingDirectory)
        {
            watcher.Changed += new FileSystemEventHandler(OnWorkingDirectoryChanged);
            watcher.Created += new FileSystemEventHandler(OnWorkingDirectoryChanged);
            watcher.Error += new ErrorEventHandler(OnWorkingDirectoryError);
        }

        watcher.EnableRaisingEvents = true;
        }
        catch (ApplicationException ex)
        {
        throw new ProblemException(ex.Message, ex.InnerException);
        }
    }


    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    internal static void OnInputDirectoryChanged(object source, FileSystemEventArgs e)
    {
        try
        {
        System.IO.FileStream file = null;
        try
        {
            if (System.IO.File.Exists(e.FullPath) == true)
            {
            file = File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
        }
        catch (IOException)
        {
            System.Threading.Thread.Yield();
            System.Threading.Thread.Sleep(100); // hack for timing issues
            return;
        }
        finally
        {
            if (file != null)
            file.Dispose();
        }

        if (e.ChangeType == WatcherChangeTypes.Created)
        {
            System.IO.FileInfo infoFile = new System.IO.FileInfo(e.FullPath);
            if (infoFile.Exists == true)
            {
            infoFile = null;
            try
            {
                if (file != null)
                {
                file.Close();
                file.Dispose();
                }

                if (System.IO.File.Exists(e.FullPath) == true)
                {
                if (System.IO.File.Exists(System.IO.Path.Combine(workingDirectory, e.Name)) == false)
                {
                    System.IO.File.Move(e.FullPath, System.IO.Path.Combine(workingDirectory, e.Name));
                }
                }
            }
            catch (IOException)
            {
                System.Threading.Thread.Yield();
                System.Threading.Thread.Sleep(100); // hack for timing issues   
                return;
            }
            }
        }
        }
        catch (ApplicationException ex)
        {
        throw new ProblemException(ex.Message, ex.InnerException);
        }
    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    private static void OnInputDirectoryError(object source, ErrorEventArgs e)
    {
        System.IO.FileSystemWatcher inboundFSW = new System.IO.FileSystemWatcher();
        inboundFSW.EnableRaisingEvents = true;
        inboundFSW.IncludeSubdirectories = false;

        while (!inboundFSW.EnableRaisingEvents)
        {
        try
        {
            WatchFileSystem.Run(inboundFSW, inputDirectory, directoryWatchfilter);
        }
        catch
        {
            System.Threading.Thread.Sleep(5000);
        }
        }
    }


    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    internal static void OnWorkingDirectoryChanged(object source, FileSystemEventArgs e)
    {
        try
        {
        System.IO.FileStream file = null;
        try
        {   
            if (System.IO.File.Exists(e.FullPath) == true)
            {
            file = File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
        }
        catch (IOException)
        {
            System.Threading.Thread.Yield();
            System.Threading.Thread.Sleep(100); // hack for timing issues   
            return;
        }
        finally
        {
            if (file != null)
            file.Dispose();
        }

        if (e.ChangeType == WatcherChangeTypes.Created)
        {
            System.IO.FileInfo infoFile = new System.IO.FileInfo(e.FullPath);
            if (infoFile.Exists == true)
            {
            infoFile = null;
                try
                {
                if (file != null)
                {
                    file.Close();
                    file.Dispose();
                }

                if (System.IO.File.Exists(e.FullPath) == true)
                {
                    generateFiles(System.IO.Path.Combine(workingDirectory, e.Name));
                }
                }
                catch (IOException)
                {
                System.Threading.Thread.Yield();
                System.Threading.Thread.Sleep(100); // hack for timing issues  
                return;
                }
            }
        }
        }
        catch (ApplicationException ex)
        {
        throw new ProblemException(ex.Message, ex.InnerException);
        }
    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    private static void OnWorkingDirectoryError(object source, ErrorEventArgs e)
    {
        System.IO.FileSystemWatcher workingFSW = new System.IO.FileSystemWatcher();
        workingFSW.EnableRaisingEvents = true;
        workingFSW.IncludeSubdirectories = false;

        while (!workingFSW.EnableRaisingEvents)
        {
        try
        {
            WatchFileSystem.Run(workingFSW, workingDirectory, directoryWatchfilter);
        }
        catch
        {
            System.Threading.Thread.Sleep(5000);
        }
        }
    }
    }
}

どんな助けでも大歓迎です。

4

1 に答える 1

1

それでも答えが見つからない場合は、次を参照してください: FileSystemWatcher の問題

代わりにポーリング システムの使用を検討しましたか? 新しいスレッドを作成し、新しいファイルを確認してコピー/移動しThread.Sleep()、選択した特定の時間間隔でスレッドをスリープ状態にするために使用できます。そして、このプロセスをループします。

于 2013-02-26T22:07:55.953 に答える