c语言监控文件变化,用delphi监控文件修改时间是否发生变化并作出自动处理

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, ExtCtrls, StdCtrls;

type

TForm1 = class(TForm)

tmrl: TTimer;

mmo1: TMemo;

edtT1: TEdit;

edtT3: TEdit;

procedure tmrlTimer(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

function CovFileDate(Fd:_FileTime):TDateTime;

var

Tct:_SystemTime;

Temp:_FileTime;

begin

FileTimeToLocalFileTime(Fd,Temp);

FileTimeToSystemTime(Temp,Tct);

CovFileDate:=SystemTimeToDateTime(Tct);

end;

procedure TForm1.tmrlTimer(Sender: TObject);

const

Model='HH:mm:ss';

var

SysTime: Windows.TSystemTime;

Tp:TSearchRec;

T1,T2,T3:String;

Result:TDateTime;

begin

if FileExists('js.txt') then

begin

FindFirst('js.txt',faAnyFile,Tp);

T2:=FormatDateTime(Model,CovFileDate(Tp.FindData.ftLastWriteTime));

mmo1.Lines.Append(T2);

FindClose(Tp);

T1:=T2;

edtT1.Text:=T1;

mmo1.Lines.Append('循环中T1=T2='+T1);

if(T1<>edtT3.Text) then //T3=''

begin

mmo1.Lines.Append(T1+'变更一次!'); //

T3:=T1;

edtT3.Text:=T3;

end;

end;

end;

end.

算法:

1.定义一个timer每隔几秒自动执行一次;

2.检查文件是否存在,如果存在

3.每隔几秒就获取一次修改时间

4.和初始变量对比,如果值已经发生变化,作出相应处理,并将当前值计入新的edit中

5.将重新获取的值与edit.text值进行对比

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要输出新建文件的名字,你可以在监控到目录变化事件后,调用FindNextChangeNotification()函数获取目录变化信息,然后从信息中获取新创建的文件名。下面是一个示例代码: ``` #include <stdio.h> #include <windows.h> void monitorDirectoryChanges(const char* path) { HANDLE dirHandle = FindFirstChangeNotification(path, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME); if (dirHandle == INVALID_HANDLE_VALUE) { printf("Failed to monitor directory changes\n"); return; } while (TRUE) { DWORD waitStatus = WaitForSingleObject(dirHandle, INFINITE); if (waitStatus == WAIT_OBJECT_0) { char buffer[1024]; DWORD bufferSize = sizeof(buffer); DWORD bytesReturned = 0; BOOL success = ReadDirectoryChangesW( dirHandle, buffer, bufferSize, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME, &bytesReturned, NULL, NULL ); if (success) { FILE_NOTIFY_INFORMATION* fileInfo = (FILE_NOTIFY_INFORMATION*) buffer; while (fileInfo) { printf("A new file has been created in %s: %.*ls\n", path, fileInfo->FileNameLength / 2, fileInfo->FileName); fileInfo = fileInfo->NextEntryOffset > 0 ? (FILE_NOTIFY_INFORMATION*) ((BYTE*) fileInfo + fileInfo->NextEntryOffset) : NULL; } } else { printf("Failed to read directory changes\n"); break; } FindNextChangeNotification(dirHandle); } else { printf("Failed to wait for directory change notification\n"); break; } } FindCloseChangeNotification(dirHandle); } int main() { char path[] = "C:\\Some\\Directory"; monitorDirectoryChanges(path); return 0; } ``` 在上面的代码中,我们调用了ReadDirectoryChangesW()函数来读取目录变化信息。该函数会返回一个指向FILE_NOTIFY_INFORMATION结构体数组的指针,每个结构体对应一个目录变化事件,其中包含了新创建文件文件名信息。我们可以通过遍历这个数组来获取所有目录变化事件的信息,从而输出新创建文件的名字。注意,这里使用了Unicode版本的ReadDirectoryChanges()函数,因为它可以正确处理中文字符。如果要处理英文字符,可以使用ReadDirectoryChangesA()函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值