CFileStatus Status;
if (CFile::GetStatus(m_Location,Status))
{
Status.m_ctime = newCreateTime;
Status.m_mtime = newLastTime;
CFile::SetStatus(m_Location,Status);
}如果这里的m_Location是个文件,那么执行没有问题,文件属性被改成Status了,但是如果是个文件夹,那么会弹出提示"对 一未命名文件 的存取被拒绝"
这是为什么?..难道不能用SetStatus修改文件夹属性?

解决方案 »

  1.   

    文件夹请直接使用API操作。
    void PASCAL CFile::SetStatus(LPCTSTR lpszFileName, const CFileStatus& status)
    {
    DWORD wAttr;
    FILETIME creationTime;
    FILETIME lastAccessTime;
    FILETIME lastWriteTime;
    LPFILETIME lpCreationTime = NULL;
    LPFILETIME lpLastAccessTime = NULL;
    LPFILETIME lpLastWriteTime = NULL; if ((wAttr = GetFileAttributes((LPTSTR)lpszFileName)) == (DWORD)-1L)
    CFileException::ThrowOsError((LONG)GetLastError(), lpszFileName); if ((DWORD)status.m_attribute != wAttr && (wAttr & readOnly))
    {
    // Set file attribute, only if currently readonly.
    // This way we will be able to modify the time assuming the
    // caller changed the file from readonly. if (!SetFileAttributes((LPTSTR)lpszFileName, (DWORD)status.m_attribute))
    CFileException::ThrowOsError((LONG)GetLastError(), lpszFileName);
    } // last modification time
    if (status.m_mtime.GetTime() != 0)
    {
    AfxTimeToFileTime(status.m_mtime, &lastWriteTime);
    lpLastWriteTime = &lastWriteTime; // last access time
    if (status.m_atime.GetTime() != 0)
    {
    AfxTimeToFileTime(status.m_atime, &lastAccessTime);
    lpLastAccessTime = &lastAccessTime;
    } // create time
    if (status.m_ctime.GetTime() != 0)
    {
    AfxTimeToFileTime(status.m_ctime, &creationTime);
    lpCreationTime = &creationTime;
    } HANDLE hFile = ::CreateFile(lpszFileName, GENERIC_READ|GENERIC_WRITE,
    FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
    NULL); if (hFile == INVALID_HANDLE_VALUE)
    CFileException::ThrowOsError((LONG)::GetLastError(), lpszFileName); if (!SetFileTime((HANDLE)hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime))
    {
    LONG sc=(LONG)::GetLastError();
    ::CloseHandle(hFile);
    CFileException::ThrowOsError(sc, lpszFileName);
    } if (!::CloseHandle(hFile))
    CFileException::ThrowOsError((LONG)::GetLastError(), lpszFileName);
    } if ((DWORD)status.m_attribute != wAttr && !(wAttr & readOnly))
    {
    if (!SetFileAttributes((LPTSTR)lpszFileName, (DWORD)status.m_attribute))
    CFileException::ThrowOsError((LONG)GetLastError(), lpszFileName);
    }
    }
    从MFC的源码看来,这个函数不能直接对目录操作。MSDN上的说法
    An application cannot create a directory by using CreateFile. The application must call CreateDirectory or CreateDirectoryEx to create a directory. To opening a directory by using CreateFile, use the FILE_FLAG_BACKUP_SEMANTICS flag.也就是自己加上FILE_FLAG_BACKUP_SEMANTICS来打开目录,其他更详细的限制请参考MSDN的提醒。
      

  2.   

    参考:
    http://digi.it.sohu.com/20060306/n242142832.shtml