Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个类调用的文件系统监视程序进程未触发事件c_C#_Events_Service_Filesystemwatcher - Fatal编程技术网

C# 从另一个类调用的文件系统监视程序进程未触发事件c

C# 从另一个类调用的文件系统监视程序进程未触发事件c,c#,events,service,filesystemwatcher,C#,Events,Service,Filesystemwatcher,我有一个windows服务,它基本上监视文件夹,将文件复制到本地目录并处理文件。该服务启动得足够好,可以调用Watcher类,并像它应该做的那样执行所有配置。因此,我的代码适用于设置。但是我得到0个事件。我想知道当我的StartFolderWatcher方法超出范围时,我的对象(即使在类级别声明)是否会以某种方式被释放 那么我需要在一个单独的线程中连续运行对象吗?如果是这样的话,我可以得到一个我应该如何完成该任务的示例 相关代码如下-如果需要更多,请询问,我会发布 static

我有一个windows服务,它基本上监视文件夹,将文件复制到本地目录并处理文件。该服务启动得足够好,可以调用Watcher类,并像它应该做的那样执行所有配置。因此,我的代码适用于设置。但是我得到0个事件。我想知道当我的StartFolderWatcher方法超出范围时,我的对象(即使在类级别声明)是否会以某种方式被释放

那么我需要在一个单独的线程中连续运行对象吗?如果是这样的话,我可以得到一个我应该如何完成该任务的示例

相关代码如下-如果需要更多,请询问,我会发布

        static class Program
        {
            static void Main(string[] args)
            {
                JobProcessor service = new JobProcessor();

                if (Environment.UserInteractive)
                {                 
                    service.RunAsConsole(args);
                }
           }

            private static bool IsDebugMode(string[] args)
            {
                if (args == null) return false;
                if (args[0].ToLower() == "/debug") return true;
                return false;
            }

    }

        public partial class JobProcessor : ServiceBase
        {
            string[] folders = new string[] {"InBoxFolder"};

            HotFolderWatch HFW = new HotFolderWatch();

            public JobProcessor()
            {
                InitializeComponent();

            }

            protected override void OnStart(string[] args)
            {

                StartFolderWatcher();
            }

            public void StartFolderWatcher()
            {
                FileWatcherInfo[] ServiceWatchers = new FileWatcherInfo[4];

                ServiceConfiguration sc = new ServiceConfiguration();

                for (int i = 0; i < folders.Length; i++)
                {
                    ServiceWatchers[i] = sc.GetWatchFolderSettings(folders[i]);
                }

                HFW = new HotFolderWatch(ServiceWatchers[0]);

                HFW.ReadyToProcess += ReadyToProcess;
                HFW.InBoxFolderDisconnected += OnInBoxFolderDisconnected;
                HFW.LocalFolderDisconnected += OnLocalFolderDisconnected;
                HFW.ProcessFolderDisconnected += OnProcessFolderDisconnected;

            }


            public void RunAsConsole(string[] args)
            {
              OnStart(args);
              Console.WriteLine("Press any key to exit...");
              Console.ReadLine();
              OnStop();
            }

         }


            public HotFolderWatch(FileWatcherInfo inbox)
            {


                this.InboxCacheTimeMilliseconds = inbox.CacheTimeMilliseconds;
                this.InBoxFolder = inbox.Folder.Trim();
                this.InboxFileFilter = inbox.Filter.Trim();
            SetInboxWatcher();

           }

            private void SetInboxWatcher()
            {
                InBoxWatcher = new FileSystemWatcher(InBoxFolder, InboxFileFilter);
                InBoxWatcher.IncludeSubdirectories = false;
                InBoxWatcher.NotifyFilter =
 NotifyFilters.LastAccess | NotifyFilters.LastWrite;

                InboxCache = MemoryCache.Default;
                InboxCachePolicy = new CacheItemPolicy()
                {
                    RemovedCallback = OnRemovedFromInBoxCache
                };

                InBoxWatcher.Created += new FileSystemEventHandler(OnInBoxChanged);
                InBoxWatcher.EnableRaisingEvents = true;
            }

            private void OnInBoxChanged(object source, FileSystemEventArgs e)
            {
                InboxCachePolicy.AbsoluteExpiration =

 DateTimeOffset.Now.AddMilliseconds(InboxCacheTimeMilliseconds);
                InboxCache.AddOrGetExisting(e.Name, e, InboxCachePolicy);
            }    
    }

原来FileSystemWatcher的默认构造函数默认使用过滤器文件名,我想我会得到最后一次写入或最后一次访问,而不必担心自己的名字。 我从这里得到了很好的信息:

链接中的文章帮助我解决了我的问题

我必须将NotifyFilters.FileName存储为其中一个过滤器

private void CreateWatcher()
{

    FileSystemWatcher fsw = new FileSystemWatcher(@"C:\Tests\JobQueue\InFolder","*.txt");
    fsw.Created += Fsw_Created;
    fsw.EnableRaisingEvents = true;
}

private void Fsw_Created(object sender, FileSystemEventArgs e)
{
    string ex = e.FullPath;
    WatcherChangeTypes ctmp = e.ChangeType;
   // throw new NotImplementedException();
}