获取文件属性

来源:互联网 发布:通达信引用60分钟数据 编辑:程序博客网 时间:2024/05/16 18:18

// 获取指定文件夹的时间属性,入口参数DirName指定了待处理的文件夹,stime为一
// 指向SYSTEMTIME结构的指针


BOOL GetDirTime(CString DirName, SYSTEMTIME &stime){
// 打开文件夹
HANDLE hDir = CreateFile (DirName, GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_DELETE,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);


FILETIME lpCreationTime; // 文件夹的创建时间
FILETIME lpLastAccessTime; // 对文件夹的最近访问时间
FILETIME lpLastWriteTime; // 文件夹的最近修改时间
// 获取文件夹时间属性信息
if (GetFileTime(hDir, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime)){
FILETIME ftime;
FileTimeToLocalFileTime(&lpLastWriteTime, &ftime); // 转换成本地时间
FileTimeToSystemTime(&ftime, &stime); // 转换成系统时间格式
}
CloseHandle(hDir); // 关闭打开过的文件夹
return retval;
}

// 设置指定文件夹的时间属性,入口参数DirName指定了待处理的文件夹,new_time
// 为一指向SYSTEMTIME结构的指针

BOOL  SetDirTime(CString DirName, SYSTEMTIME new_stime){

// 打开目录的Win32 API调用


HANDLE hDir = CreateFile(DirName, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_DELETE,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);


FILETIME lpCreationTime; // 文件夹的创建时间
FILETIME lpLastAccessTime; // 对文件夹的最近访问时间
FILETIME lpLastWriteTime; // 对文件夹的最近修改时间
SystemTimeToFileTime(&new_stime, &lpCreationTime); // 转换成文件时间格式
SystemTimeToFileTime(&new_stime, &lpLastAccessTime);
SystemTimeToFileTime(&new_stime, &lpLastWriteTime);
// 设置文件夹的时间属性


BOOL retval = SetFileTime(hDir, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime);
CloseHandle(hDir); // 关闭文件夹
return retval;
}

至此,可以很方便的通过调用GetDirTime()和SetDirTime()函数来实现对任意指定文件夹时间属性的获取与设置,具体为:

SYSTEMTIME stime; // 系统时间结构对象
if (GetDirTime(m_Path, stime))
{
 // 如果获取文件夹时间属性成功,获取到的时间信息将保存在stime结构对象中
 ……
 // 如果需要可以对获取到的时间属性进行修改,也可以保留不变
 ……
 // 将修改后的时间属性回写到文件夹
 SetDirTime(m_Path, stime);
}

void CTestDlg::OnOK()
{

HANDLE   hFile;  
WIN32_FIND_DATA   wfd;  
SYSTEMTIME   systime;  
FILETIME   localtime;  
char   stime[32];     //输出时间  
memset(&wfd,   0,   sizeof(wfd));  
   
if((hFile=FindFirstFile("D://邮件头.pdf",   &wfd))==INVALID_HANDLE_VALUE)  
{  
char   c[2];  
DWORD   dw=GetLastError();  
wsprintf(c,   "%d",   dw);  
AfxMessageBox(c);  
return   ;//失败  
}  
//ok,转换时间  
FileTimeToLocalFileTime(&wfd.ftLastWriteTime,&localtime);  
FileTimeToSystemTime(&localtime,&systime);  
sprintf(stime,"%4d-%02d-%02d   %02d:%02d:%02d",  
            systime.wYear,systime.wMonth,systime.wDay,systime.wHour,  
            systime.wMinute,systime.wSecond);  
AfxMessageBox(stime);  

}

 

time(&tm_now);
    int time_file_exit = tm_now - tm;
    if ( time_file_exit > 5*60*60 )
        remove(filename);

HANDLE hDir = CreateFile ((LPCWSTR)filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, 
                 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
    FILETIME creat_time;
    FILETIME acess_time;
    FILETIME modify_time;
    SYSTEMTIME stime;
    GetFileTime(hDir, &creat_time, &acess_time, &modify_time);
    FileTimeToSystemTime(&modify_time, &stime);
    LONGLONG llTime;
    ULARGE_INTEGER ui;
    ui.LowPart = modify_time.dwLowDateTime;
    ui.HighPart = modify_time.dwHighDateTime;
    llTime = (modify_time.dwHighDateTime << 32) + modify_time.dwLowDateTime;
    *tm = (DWORD)((LONGLONG)(ui.QuadPart - 116444736000000000) / 10000000);
    CloseHandle(hDir);