[英]std::filesystem::last_write_time to FILETIME
I am attempting to send over the FILETIME
of a file to my server, which is written in C#.我正在尝试将文件的
FILETIME
发送到我的服务器,该服务器是用 C# 编写的。 From there, I can use the function DateTime.FromFileTime()
to parse the file's time.从那里,我可以使用函数
DateTime.FromFileTime()
来解析文件的时间。 I am aware there is a Win32 API called GetFileTime()
, but in the interest of saving lines of code, I was wondering if it was possible to use std::filesystem
and somehow convert it to FILETIME
?我知道有一个名为
GetFileTime()
的 Win32 API,但为了节省代码行,我想知道是否可以使用std::filesystem
并以某种方式将其转换为FILETIME
?
I have tried casting like so:我试过像这样铸造:
f.lastwrite = entry.last_write_time();
But this cast is not allowed, unfortunately.但不幸的是,这种演员阵容是不允许的。 Is it possible to convert the
last_write_time()
to FILETIME
so it can be sent over, or is that not possible?是否可以将
last_write_time()
转换为FILETIME
以便可以将其发送过来,还是不可能?
You can convert the last_write_time()
value to std::time_t
viastd::chrono::file_clock::to_sys()
andstd::chrono::system_clock::to_time_t()
, and then convert time_t
to FILETIME
using simple arithmetic .您可以通过
std::chrono::file_clock::to_sys()
和std::chrono::system_clock::to_time_t()
将last_write_time()
值转换为std::time_t
,然后使用简单的算术将time_t
转换为FILETIME
。
For example:例如:
#include <windows.h>
#include <filesystem>
#include <chrono>
void stdFileTimeType_to_FILETIME(const std::filesystem::file_time_type &ftime, FILETIME &ft)
{
std::time_t t = std::chrono::system_clock::to_time_t(
std::chrono::file_clock::to_sys(ftime)
);
ULARGE_INTEGER ul;
ul.QuadPart = (t * 10000000LL) + 116444736000000000LL;
ft.dwLowDateTime = ul.LowPart;
ft.dwHighDateTime = ul.HighPart;
}
FILETIME lastwrite;
stdFileTimeType_to_FILETIME(entry.last_write_time(), lastwrite);
UPDATE: Alternatively, have a look at GetFileAttributesEx()
, which can retrieve FILETIME ftLastWriteTime
(amongst other things) given a file path string instead of an open file HANDLE
, as with GetFileTime()
:更新:或者,看看
GetFileAttributesEx()
,它可以检索FILETIME ftLastWriteTime
(除其他外)给定文件路径字符串而不是打开文件HANDLE
,与GetFileTime()
一样:
#include <windows.h>
#include <filesystem>
#include <chrono>
void getLastWriteTime_as_FILETIME(const std::filesystem::path &filePath, FILETIME &ft)
{
WIN32_FILE_ATTRIBUTE_DATA fad;
if (GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &fad))
ft = fad.ftLastWriteTime;
else
ft.dwLowDateTime = ft.dwHighDateTime = 0;
}
FILETIME lastwrite;
getLastWriteTime_as_FILETIME(entry.path(), lastwrite);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.