做一个小项目时要求输出文件的名称、属性、时间、大小等信息,然后在论坛里看到大神推荐CFileStatus可以获取文件信息,在这里总结记录一下,可能不全,欢迎指出错误以及评论补充。
首先FileStatus是一个结构体,此结构体存储了文件的状态信息。
下面是其结构体成员,来源于CSDN:
CTime m_ctime The date and time the file was created.
表示文件的创建时间
CTime m_mtime The date and time the file was last modified.
表示文件的修改时间
CTime m_atime The date and time the file was last accessed for reading.
表示文件的最后访问时间
ULONGLONG m_size The logical size of the file in bytes, as reported by the DIR command.
表示文件的逻辑大小
BYTE m_attribute The attribute byte of the file.
表示文件的系统属性
char m_szFullName[_MAX_PATH] The absolute filename in the Windows character set.
表示文件的绝对路径
示例如下:
CListCtrl m_list;
m_list.InsertColumn( 0, "文件大小", LVCFMT_CENTER, rect.right /4 );
m_list.InsertColumn( 1, "文件类型" , LVCFMT_CENTER, rect.right /4);
m_list.InsertColumn( 2, "修改日期" , LVCFMT_CENTER, rect.right /4);
CFileStatus status;
CFile::GetStatus(tempPath + finder.GetFileName(), status);
CString strSize;
strSize.Format(_T("%d KB"),status.m_size/1024);
m_list.SetItemText(i,0,strSize);
CString strModifyTime = status.m_mtime.Format("%Y-%m-%d %H:%M:%S");
m_list.SetItemText(i,1,strModifyTime);
CString strCreateTime = status.m_ctime.Format("%Y-%m-%d %H:%M:%S");
m_list.SetItemText(i,2,strCreateTime);
上面的示例中,前四行是调用CListCtrl创建一个标题行,效果如下:
然后在①处,建立一个从CFileStatus对象并根据路径名(tempPath)与文件名(finder.GetFileName())将对应的文件信息返回给status,在②③④处再将其在对应位置显示出来。