NTFS 文件系统时间

发布于 2024-09-12 08:54:33 字数 2024 浏览 4 评论 0原文

我在windows xp上测试过这个。

如果我

  1. 创建一个文件。
  2. 写入文件。
  3. 关闭文件。(然后,文件的 LastWriteTime 被更改)

但是如果我

  1. 创建一个文件。
  2. 设置文件的 LastFileTime。
  3. 通过调用 GetFileTime 检查时间(然后,文件的 LastWriteTime 被更改)
  4. 休眠 20 秒。
  5. 写入文件。
  6. 睡20秒。
  7. 关闭文件。
  8. 通过调用 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

  1. Create a file.
  2. Write to the file.
  3. Close the file.(Then, the file's LastWriteTime is changed)

But if I do

  1. Create a file.
  2. Set LastFileTime of the file.
  3. Examine the time by calling GetFileTime (Then, the file's LastWriteTime is changed)
  4. Sleep 20 seconds.
  5. Write to the file.
  6. Sleep 20 seconds.
  7. Close the file.
  8. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-09-19 08:54:34

尝试在更改时间戳后调用 FlushFileBuffers(h) 或在打开文件期间使用 FILE_FLAG_WRITE_THROUGHFILE_FLAG_NO_BUFFERING 标志。请参阅 http://msdn.microsoft.com/en-us/库/aa363858.aspx#caching_behavior

通过的直写请求
FILE_FLAG_WRITE_THROUGH 也会导致
NTFS 刷新任何元数据更改,
例如时间戳更新或
重命名操作,其结果是
处理请求。为了这
原因,FILE_FLAG_WRITE_THROUGH
标志经常与
FILE_FLAG_NO_BUFFERING 标志作为
替代调用
每个之后的 FlushFileBuffers 函数
写,这可能会导致不必要的
绩效处罚。使用这些
旗帜在一起可以避免这些处罚。
有关的一般信息
文件和元数据的缓存,请参阅
文件缓存。

Try to call FlushFileBuffers(h) after changing the time stamps or use FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING flag during opening of the file. See in http://msdn.microsoft.com/en-us/library/aa363858.aspx#caching_behavior:

A write-through request via
FILE_FLAG_WRITE_THROUGH also causes
NTFS to flush any metadata changes,
such as a time stamp update or a
rename operation, that result from
processing the request. For this
reason, the FILE_FLAG_WRITE_THROUGH
flag is often used with the
FILE_FLAG_NO_BUFFERING flag as a
replacement for calling the
FlushFileBuffers function after each
write, which can cause unnecessary
performance penalties. Using these
flags together avoids those penalties.
For general information about the
caching of files and metadata, see
File Caching.

私藏温柔 2024-09-19 08:54:34

引用文档

LastWriteTime 的值
如果当前属性被预先缓存
FileSystemInfo 对象的实例
从以下任一情况返回
目录信息方法:

[...]

要获取最新值,请调用
刷新方法。

Quoth the documentation:

The value of the LastWriteTime
property is pre-cached if the current
instance of the FileSystemInfo object
was returned from any of the following
DirectoryInfo methods:

[...]

To get the latest value, call the
Refresh method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文