C语言 监控目录变化获取不到新名称

发布于 2022-09-04 14:51:52 字数 7214 浏览 24 评论 0

define _WIN32_WINNT 0x0500

include

include

include

int writeLogs(char file,char str,char *mode);
int writeContent(FILE file,char content);
DWORD WINAPI WatchChanges(LPVOID lpParameter);
FILE_NOTIFY_INFORMATION renamefiles(FILE_NOTIFY_INFORMATION p,char *result);
char *logs="E:/APP/Python/logs.txt";
HANDLE handle_directory;
char *logsmode="a+";
typedef struct ThreadParameter
{

char *in_directory;//监控的路径
FILE_NOTIFY_INFORMATION *in_out_notification;//存储监控函数返回信息地址
DWORD in_MemorySize;//传递存储返回信息的内存的字节数
DWORD *in_out_BytesReturned;//存储监控函数返回信息的字节数
DWORD *in_out_version;//返回版本信息
FILE_NOTIFY_INFORMATION *temp_notification;//备用的一个参数

}ThreadParameter;

int main()
{

char *p= "E:/APP/c";
LPCWSTR *path = p;
char notify[1024];
memset(notify,'\0',1024);
FILE_NOTIFY_INFORMATION *Notification = (FILE_NOTIFY_INFORMATION *)notify;
FILE_NOTIFY_INFORMATION *TempNotification = NULL;
LPTSTR Directory = p;
DWORD BytesReturned = 0;
DWORD version = 0;

printf("start...n");

ThreadParameter ParameterToThread={p,Notification,sizeof(notify),&BytesReturned,&version,TempNotification};
printf("first...\n");
HANDLE TrheadWatch=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WatchChanges,&ParameterToThread,0,NULL);
printf("second...\n");
WatchChanges(&ParameterToThread);
CloseHandle(TrheadWatch);
return 0;

}

DWORD WINAPI WatchChanges(LPVOID lpParameter)
{

ThreadParameter *parameter = (ThreadParameter*)lpParameter;
char *WatchDirectory = parameter->in_directory;//

// printf("%s",WatchDirectory);

int edit_flag = 0;
handle_directory=CreateFile(WatchDirectory,FILE_LIST_DIRECTORY,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED,NULL);

if(handle_directory == INVALID_HANDLE_VALUE)
{
    DWORD ERROR_DIR=GetLastError();
}
//printf("get%s",parameter->in_directory);
//the state of the ReadDirectoryChangesW

BOOL watch_state;

while (TRUE)
{
    watch_state = ReadDirectoryChangesW(handle_directory,
        (LPVOID)parameter->in_out_notification,
        parameter->in_MemorySize,
        TRUE,
        FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
        (LPDWORD)parameter->in_out_BytesReturned,
        NULL,
        NULL);

if(watch_state == 0){

char *str="monitor is fail!\n";
        //writeLogs(logs,str,logsmode);
        printf("%s\n",str);

}else{

BOOL runable=0;
switch(GetLastError()){
    case ERROR_INVALID_FUNCTION:
        //writeLogs(logs,str,logsmode);
        printf("current system is unsupported!\n");
    break;
    case ERROR_NOTIFY_ENUM_DIR:
       // writeLogs(logs,str,logsmode);
       printf("out of memory!\n");
    break;
    default:
        runable=1;
    break;
}
if(runable){
    char file_name[256]={0};
    char tmp_file_name[256]={0};

        DWORD length = WideCharToMultiByte(0,0,parameter->in_out_notification->FileName,-1,NULL,0,NULL,NULL);
        char ps[256]={0};
        char tmpps[256]={0};
        if(length >= 0)
        {
            WideCharToMultiByte(0,0,parameter->in_out_notification->FileName,-1,ps,length,NULL,NULL);
            strcpy(file_name,ps);
        }
        free(&ps);
        //printf("%d %d \r\n",parameter->in_out_notification->Action,parameter->in_out_notification->NextEntryOffset);
        //printf("ACTIONS:%d\r\n",parameter->temp_notification->Action);
        int offset=0;
        int counter=0;
        do{
            parameter->in_out_notification= (FILE_NOTIFY_INFORMATION*)((BYTE*)parameter->in_out_notification + offset);
            offset=parameter->in_out_notification->NextEntryOffset;
            if(parameter->in_out_notification->Action==FILE_ACTION_ADDED){
                printf("%s : %s\n","add", file_name);
            }
            if(parameter->in_out_notification->Action==FILE_ACTION_REMOVED){
                    printf("%s : %s\n","removed", file_name);
            }
            if(parameter->in_out_notification->Action==FILE_ACTION_MODIFIED){
                printf("%s : %s\n","MODIFY", file_name);
            }
            if(parameter->in_out_notification->Action==FILE_ACTION_RENAMED_NEW_NAME){
                printf("%s : %s\n","NEW NAME", file_name);
            }
            if(parameter->in_out_notification->Action==FILE_ACTION_RENAMED_OLD_NAME){
                 printf("%s : %s\n","OLD NAME", file_name);
            }

        }while(parameter->in_out_notification->NextEntryOffset!=0);
        /*
       switch(parameter->in_out_notification->Action)
        {
            case FILE_ACTION_ADDED:
                printf("%s : %s\n","add", file_name);
                break;
            case FILE_ACTION_REMOVED:
                printf("%s : %s\n","removed", file_name);
                break;
            case FILE_ACTION_MODIFIED:
                printf("%s : %s\n","modified", file_name);
                char *mores;
                renamefiles(parameter->in_out_notification,mores);
                free(&mores);
                break;
            case FILE_ACTION_RENAMED_OLD_NAME:
                printf("...\n");
                char *res;
                renamefiles(parameter->in_out_notification,res);
                printf("%s %s -> %s\n","rename", file_name,file_name);
                free(&res);
            break;

            case FILE_ACTION_RENAMED_NEW_NAME:
                printf("%s  -> %s\n","Triggered by rename 2", file_name);
                break;
        }

*/

}

}

}
return 0;

}
FILE_NOTIFY_INFORMATION renamefiles(FILE_NOTIFY_INFORMATION p,char *result){

char *filename=(char *)malloc(sizeof(char *));
int dwNextEntryOffset=p->NextEntryOffset;
int length;
printf("next eventid:%d\r\n",dwNextEntryOffset);
if(dwNextEntryOffset!=0){
    p= (FILE_NOTIFY_INFORMATION*)((BYTE*)p + dwNextEntryOffset);
    length = WideCharToMultiByte(0,0,p->FileName,-1,NULL,0,NULL,NULL);
    if(length>0){
        WideCharToMultiByte(0,0,p->FileName,-1,filename,length,NULL,NULL);
    }

// printf("sets:%srn",filename);

   result=filename;
   printf("sets:%s\r\n",result);
}

return p;

}

int writeLogs(char file,char str,char *mode)
{

FILE *f;
char filemode[100];
sprintf(filemode,"%s",mode);
if(mode==NULL)
{
    f=fopen(file,"w");
}
else
{
    f=fopen(file,filemode);
}
int result=writeContent(f,str);
fclose(f);
return result;

}

int writeContent(FILE file,char content)
{

int res= fprintf(file,"%s",content);
fflush(file);
return res;

}

监控目录变化,如果重新修改监控目录下的文件名,只能获取到旧的文件名,不能获取到重命名后的名字,哪位大神知道原因,感激不尽

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文