Windows 获取文件操作时间的方法

来源:互联网 发布:ubuntu dhcp server 编辑:程序博客网 时间:2024/06/06 04:32

一、获取文件的修改时间

/*

typedef struct _WIN32_FIND_DATAA {
    DWORD dwFileAttributes;
    FILETIME ftCreationTime;//获取文件创建时间
    FILETIME ftLastAccessTime;//获取文件访问时间
    FILETIME ftLastWriteTime;//获取文件修改时间
    DWORD nFileSizeHigh;//获取文件大小高32位
    DWORD nFileSizeLow;//获取文件大小低32位
    DWORD dwReserved0;
    DWORD dwReserved1;
    CHAR   cFileName[ MAX_PATH ];
    CHAR   cAlternateFileName[ 14 ];
#ifdef _MAC
    DWORD dwFileType;
    DWORD dwCreatorType;
    WORD  wFinderFlags;
#endif
} WIN32_FIND_DATAA, *PWIN32_FIND_DATAA, *LPWIN32_FIND_DATAA;

*/``

举例:

#include<afx.h>////头文件include<afx.h>

int main(int argc,char *argv[])

{

WIN32_FIND_DATA ffd ;

//Filename可以为文件或者文件夹,如果为文件夹则必须保证末尾没有文件分隔符‘\\’。

char Filename[128]="D:\\文件测试.txt";
HANDLE hFind = FindFirstFile(Filename,&ffd);

printf("Filename=%s\r\n",ffd.cFileName);//打印文件名字

printf("FileSize=%d\r\n",ffd.nFileSizeLow);//如果不是文件夹:打印文件大小(单位:字节)
SYSTEMTIME stUTC, stLocal;//UTC时间变量,当地时间变量


FileTimeToSystemTime(&(ffd.ftLastWriteTime), &stUTC);//获取最后一次修改时间,获取UTC格林威治时间

printf("stUTC:%d. %d %d, %d:%d\r\n",

stUTC.wDay,

stUTC.wMonth,

stUTC.wYear,

stUTC.wHour,

stUTC.wMinute

);//显示格林威治UTC时间


SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);//时间转换,转换为当地时间

//显示当地时间
printf("stLocal:%d. %d %d, %d:%d\r\n",stLocal.wDay,stLocal.wMonth,stLocal.wYear,stLocal.wHour,stLocal.wMinute);



SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);//时间转换,转换为当地时间

printf("stLocal:%d. %d %d, %d:%d\r\n",

stLocal.wDay,

stLocal.wMonth,

stLocal.wYear,

stLocal.wHour,

stLocal.wMinute

);//显示当地时间

return 0;

}




2: 得到系统时间日期(使用GetLocalTime)  

SYSTEMTIME st;   

CString strDate,strTime;   

GetLocalTime(&st);   

strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);   

strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);

 

3.使用GetTickCount//获取程序运行时间  

 

long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)   

Sleep(500); long t2=GetTickCount();//程序段结束后取得系统运行时间(ms)   

str.Format("time:%dms",t2-t1);//前后之差即 程序运行时间   

AfxMessageBox(str);//获取系统运行时间   

long t=GetTickCount();   

CString str,str1;   

str1.Format("系统已运行 %d时",t/3600000);   

str=str1; t%=3600000;   

str1.Format("%d分",t/60000);   

str+=str1; t%=60000;   

str1.Format("%d秒",t/1000);   

str+=str1; AfxMessageBox(str);

0 0