NTFS 文件系统时间
我在windows xp上测试过这个。
如果我
- 创建一个文件。
- 写入文件。
- 关闭文件。(然后,文件的 LastWriteTime 被更改)
但是如果我
- 创建一个文件。
- 设置文件的 LastFileTime。
- 通过调用 GetFileTime 检查时间(然后,文件的 LastWriteTime 被更改)
- 休眠 20 秒。
- 写入文件。
- 睡20秒。
- 关闭文件。
- 通过调用 GetFileTime 检查时间(进程 5 的时间从未应用。为什么?)
编辑:
wstring fileName = L"D:\\testfile.txt";
HANDLE h = CreateFileW(fileName.c_str(), GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
0,
CREATE_ALWAYS,
0, 0);
FILETIME ft1, ft2, ft3;
if(!GetFileTime(h, &ft1, &ft2, &ft3))
{
return;
}
std::cout << ft3.dwHighDateTime << std::endl << ft3.dwLowDateTime << std::endl;
ft1.dwLowDateTime = 1000000;
if(!SetFileTime(h, &ft1, &ft1, &ft1))
{
return;
}
if(!GetFileTime(h, &ft1, &ft2, &ft3))
{
return;
}
std::cout << ft3.dwHighDateTime << std::endl << ft3.dwLowDateTime << std::endl;
Sleep(5000);
TCHAR buffer[] = L"Test1234567890 Test1234567890 Test1234567890 Test1234567890 Test1234567890\r\n";
DWORD writeBytes = 0;
BOOL fOk = WriteFile(h, buffer, sizeof(buffer), &writeBytes, 0);
if(!fOk)
{
return;
}
if(writeBytes != sizeof(buffer))
{
return;
}
if(!GetFileTime(h, &ft1, &ft2, &ft3))
{
return;
}
std::cout << ft3.dwHighDateTime << std::endl << ft3.dwLowDateTime << std::endl;
CloseHandle(h);
h = CreateFileW(fileName.c_str(), GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
0,
OPEN_EXISTING,
0, 0);
if(!GetFileTime(h, &ft1, &ft2, &ft3))
{
return;
}
std::cout << ft3.dwHighDateTime << std::endl << ft3.dwLowDateTime << std::endl;
CloseHandle(h);
DeleteFile(fileName.c_str());
您能解释一下吗? 谢谢。
I tested this on windows xp.
If I do
- Create a file.
- Write to the file.
- Close the file.(Then, the file's LastWriteTime is changed)
But if I do
- Create a file.
- Set LastFileTime of the file.
- Examine the time by calling GetFileTime (Then, the file's LastWriteTime is changed)
- Sleep 20 seconds.
- Write to the file.
- Sleep 20 seconds.
- Close the file.
- Examine the time by calling GetFileTime (The process5's time is never applied. Why?)
Edit:
wstring fileName = L"D:\\testfile.txt";
HANDLE h = CreateFileW(fileName.c_str(), GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
0,
CREATE_ALWAYS,
0, 0);
FILETIME ft1, ft2, ft3;
if(!GetFileTime(h, &ft1, &ft2, &ft3))
{
return;
}
std::cout << ft3.dwHighDateTime << std::endl << ft3.dwLowDateTime << std::endl;
ft1.dwLowDateTime = 1000000;
if(!SetFileTime(h, &ft1, &ft1, &ft1))
{
return;
}
if(!GetFileTime(h, &ft1, &ft2, &ft3))
{
return;
}
std::cout << ft3.dwHighDateTime << std::endl << ft3.dwLowDateTime << std::endl;
Sleep(5000);
TCHAR buffer[] = L"Test1234567890 Test1234567890 Test1234567890 Test1234567890 Test1234567890\r\n";
DWORD writeBytes = 0;
BOOL fOk = WriteFile(h, buffer, sizeof(buffer), &writeBytes, 0);
if(!fOk)
{
return;
}
if(writeBytes != sizeof(buffer))
{
return;
}
if(!GetFileTime(h, &ft1, &ft2, &ft3))
{
return;
}
std::cout << ft3.dwHighDateTime << std::endl << ft3.dwLowDateTime << std::endl;
CloseHandle(h);
h = CreateFileW(fileName.c_str(), GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
0,
OPEN_EXISTING,
0, 0);
if(!GetFileTime(h, &ft1, &ft2, &ft3))
{
return;
}
std::cout << ft3.dwHighDateTime << std::endl << ft3.dwLowDateTime << std::endl;
CloseHandle(h);
DeleteFile(fileName.c_str());
Could you explain me about this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在更改时间戳后调用
FlushFileBuffers(h)
或在打开文件期间使用FILE_FLAG_WRITE_THROUGH
和FILE_FLAG_NO_BUFFERING
标志。请参阅 http://msdn.microsoft.com/en-us/库/aa363858.aspx#caching_behavior:Try to call
FlushFileBuffers(h)
after changing the time stamps or useFILE_FLAG_WRITE_THROUGH
andFILE_FLAG_NO_BUFFERING
flag during opening of the file. See in http://msdn.microsoft.com/en-us/library/aa363858.aspx#caching_behavior:引用文档:
Quoth the documentation: