我已经搜索了很多fourm,但没有找到以下问题的任何解决方案,我希望有人能够在这个论坛中帮助我。
我有两个 FSW 的 .net windows 服务,用于监视网络上的文件夹。
文件夹结构 \NetworkDrive\NewFolder\InputDirectory \NetworkDrive\NewFolder\WorkingDirectory
当我将多个文件复制到 \NetworkDrive\NewFolder\InputDirectory 目录时,可以说 file1、file2、file3 和 file4 然后只处理 file1、file2 和 file3 并留下一个文件
下面是服务类和文件系统观察类的代码
服务等级
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);
}
}
}
}
}
任何帮助将不胜感激。