C#中利用FileSystemWatcher对单个文件内容的监视

系统 2334 0

在项目中需要用 FileSystemWatcher对单个文件内容进行监视,文件中每添加一行则需要Fire相应的事件,代码如下

    public   class  CommandWatcher
    
{


        
public   event  EventHandler < CommandEventArgs >  CommandHandler;
      

        
Fields  Fields


        
public  CommandWatcher( string  cmdfile)
        
{
            
this .fileName  =  cmdfile;
            isChangeImmediate 
=   true ;

         
        }




        
        
//
        
//  Summary:
        
//      Raises the System.IO.FileSystemWatcher.Changed event.
        
//
        
//  Parameters:
        
//    e:
        
//      A System.IO.FileSystemEventArgs that contains the event data.
         protected   void  OnChanged( object  sender, FileSystemEventArgs e)   
        
{
            ReloadFile();
        }



    
        
//
        
//  Summary:
        
//      Raises the System.IO.FileSystemWatcher.Error event.
        
//
        
//  Parameters:
        
//    e:
        
//      An System.IO.ErrorEventArgs that contains the event data.
         protected   void  OnError( object  sender, ErrorEventArgs e)
        
{

        }


        
//
        
//  Summary:
        
//      Raises the System.IO.FileSystemWatcher.Renamed event.
        
//
        
//  Parameters:
        
//    e:
        
//      A System.IO.RenamedEventArgs that contains the event data.
         protected   void  OnRenamed( object  sender, RenamedEventArgs e)
        
{

        }




        
///   <summary>
        
///  Hook up for the currently selected monitoring method.
        
///   </summary>

         public   void  Start()
        
{
            
if  (isChangeImmediate)
            
{
                
//  stop timer if running
                ClearTimer();
                
//  If a file is set and the watcher has not been set up yet then create it
                 if  ( null   !=  fileName  &&   null   ==  watcher)
                
{
                    
string  path  =  Path.GetDirectoryName(fileName);
                    
string  baseName  =  Path.GetFileName(fileName);
                    
//  Use the FileSystemWatcher class to detect changes to the log file immediately
                    
//  We must watch for Changed, Created, Deleted, and Renamed events to cover all cases
                    watcher  =   new  System.IO.FileSystemWatcher(path, baseName);
                    FileSystemEventHandler handler 
=   new  FileSystemEventHandler(OnChanged);
                    watcher.Changed 
+=  handler;
                    watcher.Created 
+=  handler;
                    watcher.Deleted 
+=  handler;
                    watcher.Renamed 
+=  OnRenamed;
                    
//  Without setting EnableRaisingEvents nothing happens
                    watcher.EnableRaisingEvents  =   true ;
                }

            }

            
else
            
{
                
//  On a timer so clear the watcher if previously set up
                ClearWatcher();
                
int  numSeconds  =   5 ;
               
                
if  ( null   !=  timer)
                
{
                    
//  Timer is already running so just make sure the interval is correct
                     if  (timer.Interval  !=   1000   *  numSeconds)
                    
{
                        timer.Interval 
=   1000   *  numSeconds;
                    }

                }

                
else
                
{
                    
//  Fire up a timer if the user entered a valid time interval
                     if  ( 0   !=  numSeconds)
                    
{
                        timer 
=   new  System.Windows.Forms.Timer();
                        timer.Interval 
=   1000   *  numSeconds;
                        timer.Tick 
+=  timerTick;
                        timer.Start();
                    }

                }

            }

            
this .ReloadFile();
        }


        
void  timerTick( object  sender, EventArgs e)
        
{
            UpdateBasedOnFileTime();
        }



        
///   <summary>
        
///  Update the log file if the modified timestamp has changed since the last time the file was read.
        
///   </summary>

         private   void  UpdateBasedOnFileTime()
        
{
            
if  ( null   !=  fileName)
            
{
                
//  This returns "12:00 midnight, January 1, 1601 A.D." if the file does not exist
                DateTime newLastModifiedTime  =  File.GetLastWriteTime(fileName);
                
if  ((newLastModifiedTime.Year  <   1620   &&  newLastModifiedTime  !=  lastModifiedTime)
                    
||  newLastModifiedTime  >  lastModifiedTime)
                
{
                    
// OnLogFileChanged();
                    ReloadFile();
                }

            }

        }

        
///   <summary>
        
///  Read the log file.
        
///   </summary>
        
///   <remarks> If the user has some text selected then keep that text selected. </remarks>

         private   void  ReloadFile()
        
{
            
if  (reloadingFile)  return //  already busy doing this
            reloadingFile  =   true ;
            
try
            
{
                
if  ( null   ==  fileName)
                
{
                   
//  textBoxContents.Text = "";
                }

                
else
                
{
                    
string  newFileLines  =   "" ;
                    lastModifiedTime 
=  File.GetLastWriteTime(fileName);  //  Remember when we last read in this file
                     long  newLength  =   0 ;   //  will be set properly later if all is well
                     bool  fileExists  =  File.Exists(fileName);
                    
bool  needToClear  =   ! fileExists;
                    
if  (fileExists)
                    
{
                        
//  It is possible the file will be in use when we read it (especially if using Immediate mode)
                        
//  So, we will try up to 5 times to access the file
                         int  count  =   0 ;
                        
bool  success  =   false ;
                        
while  (count  <   5   &&   ! success)
                        
{
                            
try
                            
{
                                
//  Open with the least amount of locking possible
                                 using  (FileStream stream  =  File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                                
{
                                    newLength 
=  stream.Length;
                                    
if  (newLength  >=  lastFileSize)
                                    
{
                                        stream.Position 
=  lastFileSize;   //  Only read in new lines added
                                    }

                                    
else
                                    
{
                                        needToClear 
=   true ;
                                    }

                                    
using  (StreamReader reader  =   new  StreamReader(stream))
                                    
{
                                        newFileLines 
=  reader.ReadToEnd();

                                        
if  (newFileLines  !=   string .Empty)
                                        
{
                                            
if  ( this .CommandHandler  !=   null )
                                                
this .CommandHandler( this new  CommandEventArgs(newFileLines));
                                        }

                                      
                                        
// string []tmp = newFileLines.Split(seprater, StringSplitOptions.RemoveEmptyEntries);

                                    }

                                }

                                success 
=   true ;
                            }

                            
catch  (IOException)
                            
{
                                System.Threading.Thread.Sleep(
50 );  //  Give a little while to free up file
                            }

                            
++ count;
                        }

                    }

                    
//  Remember the current file length so we can read only newly added log lines the next time the log file is read
                    lastFileSize  =  newLength;
                    
if (lastFileSize  >   1024   *   1024   *   2 )
                    
{
                        File.Delete(fileName);
                        File.Create(fileName);
                    }
                   
               
                    
if  ( 0   !=  newFileLines.Length)
                    
{
                        
//  See if this log file has proper cr/lf and if not convert
                         int  lastCr  =  newFileLines.LastIndexOf( ' \n ' );
                        
if  ( - 1   !=  lastCr  &&   0   <  lastCr)
                        
{
                            
if  (newFileLines[lastCr  -   1 !=   ' \r ' )
                            
{
                                
//  OK, this file only has Cr and we need to convert to CrLf
                                newFileLines  =  newFileLines.Replace( " \n " " \r\n " );
                            }

                        }

                    }

                    
                   
                }

            }

            
finally
            
{
                
//  Put clearing this flag in a finally so we know it will be cleared
                reloadingFile  =   false ;
            }

        }



        
///   <summary>
        
///  Cleanup the  <see cref="Timer"/>
        
///   </summary>

         private   void  ClearTimer()
        
{
            
if  ( null   !=  timer)
            
{
                timer.Tick 
-=  timerTick;
                timer.Dispose();
                timer 
=   null ;
            }

        }


        
///   <summary>
        
///  Cleanup the  <see cref="FileSystemWatcher"/>
        
///   </summary>

         private   void  ClearWatcher()
        
{
            
if  ( null   !=  watcher)
            
{
                FileSystemEventHandler handler 
=   new  FileSystemEventHandler(OnChanged);
                watcher.Changed 
-=  handler;
                watcher.Created 
-=  handler;
                watcher.Deleted 
-=  handler;
                watcher.Renamed 
-=  OnRenamed;
                watcher.Dispose();
                watcher 
=   null ;
            }

        }




    }

C#中利用FileSystemWatcher对单个文件内容的监视


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论