我搜索了很多四分之一,并没有找到任何解决方案来解决下面的问题,我希望有人能够在这个论坛上帮助我。
我有.net windows服务,有两个FSW监视网络上的文件夹。
文件夹的结构 \ NetworkDrive \ NewFolder \ InputDirectory \ NetworkDrive \ NewFolder \ WorkingDirectory
当我将多个文件复制到\ NetworkDrive \ NewFolder \ InputDirectory目录时,让我们说file1,file2,file3和file4然后只处理file1,file2和file3并留下一个文件
以下是服务类和filesystemwatcher类的代码
服务类
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 Class
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);
}
}
}
}
}
任何帮助将不胜感激。
答案 0 :(得分:1)
如果您还没有找到答案,请参阅: FileSystemWatcher issues
您是否考虑过使用投票系统?您可以使用创建新线程,检查新文件并复制/移动它们,然后使用Thread.Sleep()
使线程在您选择的特定时间间隔内进入休眠状态。并循环完成此过程。