C++設定檔案修改時間
阿新 • • 發佈:2019-01-27
VS2010新建控制檯空專案,加入C++程式碼:
#include <Windows.h> #include <stdio.h> bool ConvertFileTimeToLocalTime(const FILETIME *lpFileTime, SYSTEMTIME *lpSystemTime) { if (!lpFileTime || !lpSystemTime) { return false; } FILETIME ftLocal; FileTimeToLocalFileTime(lpFileTime, &ftLocal); FileTimeToSystemTime(&ftLocal, lpSystemTime); return true; } bool ConvertLocalTimeToFileTime(const SYSTEMTIME *lpSystemTime, FILETIME *lpFileTime) { if (!lpSystemTime || !lpFileTime) { return false; } FILETIME ftLocal; SystemTimeToFileTime(lpSystemTime, &ftLocal); LocalFileTimeToFileTime(&ftLocal, lpFileTime); return true; } int main() { HANDLE hFile; FILETIME ftCreate, ftAccess, ftWrite; SYSTEMTIME stCreate, stAccess, stWrite; int year, month, day; hFile = CreateFile(L"C:\\1.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if (INVALID_HANDLE_VALUE == hFile) { printf("CreateFile error: %d", GetLastError()); ExitProcess(0); } GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite); ConvertFileTimeToLocalTime(&ftCreate, &stCreate); ConvertFileTimeToLocalTime(&ftAccess, &stAccess); ConvertFileTimeToLocalTime(&ftWrite, &stWrite); printf("yyyy-MM-dd:"); scanf("%d-%d-%d", &year, &month, &day); stAccess.wYear = stWrite.wYear = year; stAccess.wMonth = stWrite.wMonth = month; stAccess.wDay = stWrite.wDay = day; ConvertLocalTimeToFileTime(&stAccess, &ftAccess); ConvertLocalTimeToFileTime(&stWrite, &ftWrite); SetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite); CloseHandle(hFile); return 0; }
只修改年月日,時分秒不變。