我有一台称量仪表,软件是厂家做的,称量记录在一个文本文件中,是按照顺序追加到文本文件中,一条称量数据作为一行追加到文本文件的尾部,我现在想这样,如果有新内容加到文本文件中,我就取最后一行,也就是最新的记录,现在的问题是:我不想通过文件字节大小和更新时间来判断文件是否有更新,我想有没有一个办法就像数据库中的触发器一样,也就是说如果这个文件内容变了,就触发,请各位高手指点啊,谢谢谢谢谢谢

解决方案 »

  1.   

    定时用GetFileSize看看大小是否改变就可以啦。
      

  2.   

    问题:我想知道一个文件是否被改变,如何监视?一个方法是使用Timer不停监测,这样不精确并且来非CPU时间,那么有没有更好的方法呢? 
    来自Luke有:TFileChangeNotificationStatus=(fnsIdle,fnsError,fnsRunning);TFileChangeNotification=recordPathName:string;WatchSubtree:boolean;NotifyFilter:DWORD;Status:TFileChangeNotificationStatus;Changes:DWORD;wndHandle:HWND;NotificationMessage:DWORD;end; TFileChangesMultipleWatcher = class(TThread)private{ Private declarations }FMutex:integer;FObjectsArray:array[0..(WAITOBJECTSCOUNT-1)]of integer;protectedprocedure Execute; override;publicNotification:TFileChangeNotification;constructor Create(Suspended:boolean);destructor Destroy; override;procedure Init;procedure Stop;end; constructor TFileChangesMultipleWatcher.Create;var guid:TGUID;begininherited;coCreateGuid(guid);FMutex:=CreateMutex(nil,False,PChar('TFCMW.'+GuidToString(guid)));WaitForSingleObject(FMutex,INFINITE);if FMutex<>0 then FObjectsArray[0]:=FMutex;FreeOnTerminate:=True;end;//destructor TFileChangesMultipleWatcher.Destroy;begininherited;{$ifdef DEBUGGING_FILECHANGES} writeln(Notification.PathName,' -DESTROY'); {$endif}end;//procedure TFileChangesMultipleWatcher.Init;beginwith Notification do beginPathName:='';WatchSubtree:=False;NotifyFilter:=0;Status:=fnsIdle;Changes:=0;wndHandle:=0;NotificationMessage:=WM_USER;end;end;//procedure TFileChangesMultipleWatcher.Stop;beginTerminate;ReleaseMutex(FMutex);end;//procedure TFileChangesMultipleWatcher.Execute;var WaitStatus,hChanges:DWORD;Go:boolean;begintry while (not Terminated) do beginwith Notification do beginhChanges:=FindFirstChangeNotification(PChar(PathName),WatchSubtree,NotifyFilter);{$ifdef DEBUGGING_FILECHANGES} writeln('Notification Handle is',hChanges); {$endif}if (hChanges<>INVALID_HANDLE_VALUE) then tryFObjectsArray[1]:=hChanges;Status:=fnsRunning;Go:=True;while (Go) do begin{$ifdef DEBUGGING_FILECHANGES} writeln('WaitForSingleObject');{$endif}WaitStatus:=WaitForMultipleObjects(WAITOBJECTSCOUNT,@FObjectsArray,False,INFINITE);file://if Terminated then exit;case WaitStatus ofWAIT_ABANDONED:begin{$ifdef DEBUGGING_FILECHANGES} writeln('WAIT_ABANDONED');{$endif}end;WAIT_OBJECT_0:beginexit;{$ifdef DEBUGGING_FILECHANGES} writeln('WAIT_OBJECT_0');{$endif}end;WAIT_OBJECT_0+1:begininc(Changes);if (wndHandle<>0) thenPostMessage(wndHandle,NotificationMessage,Changes,NotificationMessage+Changes);{$ifdef DEBUGGING_FILECHANGES} writeln('WAIT_OBJECT_1');{$endif}end;WAIT_TIMEOUT:begin{$ifdef DEBUGGING_FILECHANGES} writeln('WAIT_TIMEOUT');{$endif}end;end;Sleep(100);Go:=FindNextChangeNotification(hChanges);end;finally{$ifdef DEBUGGING_FILECHANGES} writeln('FINALLY 1'); {$endif}FindCloseChangeNotification(hChanges);end else beginStatus:=fnsError;{$ifdef DEBUGGING_FILECHANGES} writeln('STATUS ERROR'); {$endif}end;end;end;finally{$ifdef DEBUGGING_FILECHANGES} writeln('FINALLY 2'); {$endif}ReleaseMutex(FMutex);end;{$ifdef DEBUGGING_FILECHANGES} writeln('BYE!'); {$endif}end;//