使用FileSystemWatcher重复事件 [英] Duplicate Events with FileSystemWatcher

查看:122
本文介绍了使用FileSystemWatcher重复事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我正在开发用户控件.需要在以下位置检查文件
我需要在2个地方初始化FileSystemWacther事件.
1)在EndInit();
2)在属性FileNameChange

NotifyFilters: NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName

现在它使用FileSystemWacther产生重复事件.


如何避免这种情况,请帮助我,在此先感谢.

我的样本男女合用:

public Class MyWatcher:System.ComponentModel.Component,ISupportInitialize
{
私有System.ComponentModel.Container组件= null;

FileSystemWatcher watcher = new FileSystemWatcher();
private void ReleaseEvents()
        {
           if(watcher!= null)
           {
               watcher.Renamed-= new RenamedEventHandler(fileRenamed);
          &b watcher.Deleted-= new FileSystemEventHandler(fileDeleted);
               watcher.Changed-=新的FileSystemEventHandler(fileChanged);
               watcher.Created-= new FileSystemEventHandler(fileCreated);
            }
       }
       ///< summary>
       ///清理所有正在使用的资源.
        ///</summary>
        //私人布尔处置= false;

      受保护的覆盖无效void Dispose(布尔处置)
       {
          如果(处理)
           {
               if(watcher!= null)
              {
             ReleaseEvents();
watcher.Dispose();
              }
              if(components!= null)
              {
             components.Dispose();
             }
              

          }
          
           base.Dispose(处置);

        }
       #region ISupport初始化成员

      公共无效BeginInit()
       {

       }
       public void EndInit()
       {
               this.fullpath =某些路径";
FileWatcher();
       }

       字符串fullpath = null;
      字符串FullPath
       {
          得到{return fullpath; }
          设置{fullpath = value; FileWatcher();}//

        }
       public void FileWatcher()
       {


         
           if(fullpath!= null)
{
               watcher.Path = Path.GetDirectoryName(fullpath);
watcher.Filter = fileName;
             
               watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

               ReleaseEvents();

              watcher.Renamed + = new RenamedEventHandler(fileRenamed);
watcher.Deleted + = new FileSystemEventHandler(fileDeleted);
watcher.Changed + =新的FileSystemEventHandler(fileChanged);            
               watcher.Created + = new FileSystemEventHandler(fileCreated);


               watcher.EnableRaisingEvents = true;

            }

        }
      
       void fileChanged(object sender,FileSystemEventArgs e)
       {
          
               ReadMethod(e.FullPath);

         
       }
       
       void fileCreated(对象发送者,FileSystemEventArgs e)
       {


           ReadMethod(e.FullPath);


        }
       void fileDeleted(对象发送者,FileSystemEventArgs e)
       {
       
       }
       void fileRenamed(对象发送者,RenamedEventArgs e)
        {
           ReadMethod(e.FullPath);

       }
      私有无效ReadMethod(字符串路径)
       {
               //读取一些文本文件
        }
}



I am developing a user control  which needs to check file on  every operation.For this i am using FileSystemWacther.
I need to initialize FileSystemWacther events in 2 places.
1)in EndInit();
2)in a Property FileNameChange

NotifyFilters:  NotifyFilters.LastWrite | NotifyFilters.FileName |NotifyFilters.DirectoryName

now its producing duplicate events with FileSystemWacther. 


how to avoid this please help me , thanks in advance.

 My sample coed:

public Class MyWatcher : System.ComponentModel.Component,ISupportInitialize
{
 private System.ComponentModel.Container components = null;

FileSystemWatcher watcher = new FileSystemWatcher();
private void ReleaseEvents()
        {
            if (watcher != null)
            {
                watcher.Renamed -= new RenamedEventHandler(fileRenamed);
                watcher.Deleted -= new FileSystemEventHandler(fileDeleted);
                watcher.Changed -= new FileSystemEventHandler(fileChanged);
                watcher.Created -= new FileSystemEventHandler(fileCreated);
            }
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        // private bool disposed = false;

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (watcher != null)
                {
                    ReleaseEvents();
                    watcher.Dispose();
                }
                if (components != null)
                {
                    components.Dispose();
                }
              

            }
           
            base.Dispose(disposing);

        }
        #region ISupportInitialize Members

        public void BeginInit()
        {

        }
        public void EndInit()
        {
                this.fullpath ="some Path";
                 FileWatcher();
        }

        string fullpath = null;
        string FullPath
        {
            get { return fullpath; }
            set { fullpath = value; FileWatcher();}//

        }
        public void FileWatcher()
        {


          
            if (fullpath != null)
            {
                watcher.Path = Path.GetDirectoryName(fullpath);
                watcher.Filter = fileName;
               
                watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName |NotifyFilters.DirectoryName ;
                
                ReleaseEvents();

                watcher.Renamed += new RenamedEventHandler(fileRenamed);
                watcher.Deleted += new FileSystemEventHandler(fileDeleted);
                watcher.Changed += new FileSystemEventHandler(fileChanged);               
                watcher.Created += new FileSystemEventHandler(fileCreated);
               

                watcher.EnableRaisingEvents = true;

            }

        }
       
        void fileChanged(object sender, FileSystemEventArgs e)
        {
           
                 ReadMethod(e.FullPath);

          
        }
        
        void fileCreated(object sender, FileSystemEventArgs e)
        {


            ReadMethod(e.FullPath);


        }
        void fileDeleted(object sender, FileSystemEventArgs e)
        {
         
        }
        void fileRenamed(object sender, RenamedEventArgs e)
        {
            ReadMethod(e.FullPath);

        }
        private void ReadMethod(string path)
        {
                //reading some text File
        }
}

推荐答案

您好,

图像控件如何更新图像更改.我在winform上有一个图像,如果我用另一个图像更新图像,那么它应该显示新图像.
但这如何工作?
与FileWatcher有什么关系以检查图像大小,位置吗? ,创建时间?
请帮帮我!
先谢谢了.
Hi,

how does image control updates image changes .
ex. i have an image on winform  and if i update image with another image then it should display new image .
but how does this works?
is there anyhting to do with FileWatcher in order to check image size,location,creation time?
please help me!
thanks in advance.


这篇关于使用FileSystemWatcher重复事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆