简体   繁体   English

std::filesystem::last_write_time 到 FILETIME

[英]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.

相关问题 g ++在std :: filesystem :: last_write_time中打破了变化 - g++ breaking change in std::filesystem::last_write_time boost :: filesystem :: last_write_time在哪里? - Where is boost::filesystem::last_write_time? 使用boost :: filesystem :: last_write_time获取锁定文件夹的修改时间 - Get modification time of locked folder with boost::filesystem::last_write_time 成功执行boost :: filesystem :: last_write_time需要哪些权限? - Which permissions are required to succesfully execute boost::filesystem::last_write_time? 上次写入FILETIME总是返回当前时间 - Last write FILETIME always returning current time Boost last_write_time更改最后写入时间值 - Boost last_write_time changes the last-write-time value "将上次修改时间的字符串表示形式转换回其类型(std::filesystem::file_time_type)" - Converting string representation of last modification time back to its type(std::filesystem::file_time_type) 如何获取 std::filesystem::path 中的最后一个目录? - How to get the last directory in a std::filesystem::path? 如何使用FILETIME / SYSTEMTIME或boost在C ++中设置文件在Win32上的最后访问时间? - How to set a file's last access time on Win32 in C++ using FILETIME/SYSTEMTIME or boost? 将FILETIME转换为可移植的时间单位 - Convert FILETIME to portable time unit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM