试用ReadDirectoryChangesW

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

int __cdecl main( int argc, char* argv[] )
{
    CHAR   FilePath[MAX_PATH] = {0};        
    CHAR   buffer[1024] = {0};    
    HANDLE hDirectory; 
    int    iRet; 
    BOOL   bRet;
    PFILE_NOTIFY_INFORMATION NotifyInfo = (PVOID)buffer; 

    if ( argc == 1 )
    {
        GetCurrentDirectoryA( MAX_PATH, FilePath );
    }
    else
    {
        strcpy( FilePath, argv[1] );
    }

    hDirectory = CreateFileA( FilePath, 
                    FILE_LIST_DIRECTORY,
                    FILE_SHARE_READ|FILE_SHARE_WRITE, 
                    NULL, 
                    OPEN_EXISTING, 
                    FILE_ATTRIBUTE_DIRECTORY|FILE_FLAG_BACKUP_SEMANTICS, 
                    NULL ); 
    
    while ( hDirectory != INVALID_HANDLE_VALUE ) 
    { 
        RtlZeroMemory( buffer, sizeof(buffer) );
        bRet = ReadDirectoryChangesW( hDirectory, 
                    NotifyInfo, 
                    sizeof(buffer), 
                    FALSE, 
                    FILE_NOTIFY_CHANGE_LAST_WRITE|FILE_NOTIFY_CHANGE_LAST_ACCESS|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_FILE_NAME ,  
                    (PDWORD)&iRet, 
                    NULL, 
                    NULL);
        if( bRet ) 
        { 
            while ( TRUE )
            {
                RtlZeroMemory( FilePath, sizeof(FilePath) );
                iRet = WideCharToMultiByte( CP_ACP, 
                                    0, 
                                    NotifyInfo->FileName, 
                                    wcslen(NotifyInfo->FileName), 
                                    FilePath, 
                                    MAX_PATH, 
                                    NULL, 
                                    NULL );
                
                if( iRet <= 0 )break;
                
                switch( NotifyInfo->Action ) 
                { 
                case FILE_ACTION_ADDED: 
                    printf("Directory/File added    : %s\r\n", FilePath);
                    break; 
                case FILE_ACTION_REMOVED:
                    printf("Directory/File removed  : %s\r\n", FilePath);
                    break; 
                case FILE_ACTION_MODIFIED: 
                    printf("Directory/File modified : %s\r\n", FilePath);
                    break; 
                case FILE_ACTION_RENAMED_OLD_NAME: 
                    printf("Directory/File old name : %s\r\n", FilePath);
                    break; 
                case FILE_ACTION_RENAMED_NEW_NAME: 
                    printf("Directory/File new name : %s\r\n", FilePath);
                    break; 
                default: 
                    break; 
                }

                if ( NotifyInfo->NextEntryOffset == 0 )
                {
                    break;
                }

                (PUCHAR)NotifyInfo = (PUCHAR)NotifyInfo + NotifyInfo->NextEntryOffset ;
            }
        }
    }

    if (  hDirectory != INVALID_HANDLE_VALUE )
    {
        CloseHandle(  hDirectory );
    }

    return 0;
}


你可能感兴趣的:(试用ReadDirectoryChangesW)