CreateFile GetFileTIme SetFileTime

后端 未结 2 970
花落未央
花落未央 2021-01-05 19:33

I\'m having trouble using GetFileTime and SetFileTime when it comes to directories. Specifically I think my problem is that I am new to the WinAPI and I don\'t think I\'m

相关标签:
2条回答
  • 2021-01-05 20:11

    Here is a code example on how to set a directory's date based on a DOS date timestamp.

    int Directory_SetDosTime(char *Path, unsigned int DosDateTime)
    {
        FILETIME LocalTime, FileTime;
        HANDLE Handle;
        SYSTEMTIME SystemTime;
    
    
        DosDateTimeToFileTime((DosDateTime >> 16), DosDateTime, &LocalTime);
        LocalFileTimeToFileTime(&LocalTime, &FileTime);
        FileTimeToSystemTime(&FileTime, &SystemTime);
    
        Handle = CreateFile(Path, GENERIC_WRITE, FILE_SHARE_WRITE,
                        NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
    
        if (Handle == INVALID_HANDLE_VALUE)
        {
            //Unable to open directory
            return FALSE;
        }
    
        if (SetFileTime(Handle, &FileTime, &FileTime, &FileTime) == 0)
        {
            //Unable to set directory time
            CloseHandle(Handle);
            return FALSE;
        }
    
        CloseHandle(Handle);
        return TRUE;
    }
    
    0 讨论(0)
  • 2021-01-05 20:19

    Answer #2: To use CreateFile to get a handle to a directory, you need to use the FILE_FLAG_BACKUP_SEMANTICS flag. Using your example:

    h1 = CreateFile(itemA, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
    

    I would guess this would work for Answer #4 as well, but I haven't tried it to confirm.

    0 讨论(0)
提交回复
热议问题