1

ReadDirectoryChangeはWIN32 APIで使用します。10 個のフォルダーをコピーした後、私のプログラムは更新を 10 回実行しています。ただし、一度だけリフレッシュしたい。手伝って頂けますか?これが私のコードです:

bool FolderWatcher::Loop ()
{
    // declare a variable and using it to return
    int i = 0;
    // convert from WCHAR to LPWSTR
    LPWSTR ptr = _folder;
    char buf[256 * (sizeof(FILE_NOTIFY_INFORMATION) + MAX_PATH * sizeof(WCHAR))] = {0};
    DWORD bytesReturned = 0;
    BOOL result = FALSE;
    FILE_NOTIFY_INFORMATION *fni = NULL;
    // using CreateFile to choose folder need to watch
    HANDLE hDir = CreateFileW( 
        ptr,
        FILE_LIST_DIRECTORY | 
        STANDARD_RIGHTS_READ,
        //Enables subsequent open operations on a file or device to request read access.
        FILE_SHARE_READ | 
        //Enables subsequent open operations on a file or device to request write access.
        FILE_SHARE_WRITE | 
        //Enables subsequent open operations on a file or device to request delete access.
        FILE_SHARE_DELETE,
    NULL,
    //Opens a file or device, only if it exists.
    OPEN_EXISTING,
    FILE_FLAG_BACKUP_SEMANTICS,
    NULL);
    // check if directory is not exists
    if (!hDir || hDir == INVALID_HANDLE_VALUE)
    {
        i = 0;
    }
    else
    {
        for (;;)
        {
            // call ReadDirectoryChangeW to watch folder
            result = ReadDirectoryChangesW(hDir,
            buf,
            sizeof(buf) / sizeof(*buf),
            FALSE, /* monitor the entire subtree */
                FILE_NOTIFY_CHANGE_FILE_NAME |
                FILE_NOTIFY_CHANGE_DIR_NAME |
                FILE_NOTIFY_CHANGE_ATTRIBUTES |
                FILE_NOTIFY_CHANGE_SIZE | 
                /*FILE_NOTIFY_CHANGE_LAST_WRITE | 
                FILE_NOTIFY_CHANGE_LAST_ACCESS |*/
                FILE_NOTIFY_CHANGE_CREATION |
                FILE_NOTIFY_CHANGE_SECURITY,
            &bytesReturned,
            NULL,
            NULL);
                if (result && bytesReturned)
                {
                    wchar_t filename[MAX_PATH];
                    wchar_t action[256];
                    for (fni = (FILE_NOTIFY_INFORMATION*)buf; fni; )
                    {
                        switch (fni->Action)
                        {
                             // excute if a folder/ text docu is added
                            case FILE_ACTION_ADDED:
                            {
                                i = 1;
                                PostMessage (_hwndNotifySink, WM_FOLDER_CHANGE, 0, (LPARAM) _folder);
                            }
                                break;
                            // excute if a folder/ text docu is removed
                            case FILE_ACTION_REMOVED:
                            {
                                i = 1;
                                PostMessage (_hwndNotifySink, WM_FOLDER_CHANGE, 0, (LPARAM) _folder);
                            }
                                break;
                            case FILE_ACTION_MODIFIED:
                            {
                                i = 1;
                                if(lstrcmp(fni->FileName, L"service.log") != 0)
                                PostMessage (_hwndNotifySink, WM_FOLDER_CHANGE, 0, (LPARAM) _folder);
                                break;
                            }
                            // excute if a folder/ text docu is renamed
                            case FILE_ACTION_RENAMED_OLD_NAME:
                            {
                                i = 1;
                                PostMessage (_hwndNotifySink, WM_FOLDER_CHANGE, 0, (LPARAM) _folder);
                                break;
                            }
                            // excute if a folder/ text docu is renamed new name
                            case FILE_ACTION_RENAMED_NEW_NAME:
                            {
                                i = 1;
                                PostMessage (_hwndNotifySink, WM_FOLDER_CHANGE, 0, (LPARAM) _folder);
                            }
                                break;
                        }
                    if (fni->NextEntryOffset)
                    {
                        char *p = (char*)fni;
                        fni = (FILE_NOTIFY_INFORMATION*)(p + fni->NextEntryOffset);
                    }
                    else
                    {
                        fni = NULL;
                    }
                }
            }
            else
            {
                i = 0;
            }
        }   
    CloseHandle(hDir); // close handle
    }
    return i; // return value of i
    }

の場合WM_FOLDER_CHANGEは、refresh 関数を呼び出します。

4

0 に答える 0