Delphi:SetFileDate 创建错误的 LastWriteTime(夏季/冬季)

标签 delphi datetime file-attributes

我正在从我的服务器下载一个文件(我只获取最后写入时间属性的字节和日期时间),下载数据后我在本地计算机上创建一个新文件并希望设置最后写入时间属性。 为此,我使用以下方法:

procedure SetFileDate(const FileName: string; NewDate: TDateTime);
var
    FileDate, FileHandle: Integer;
begin
    try
        FileDate := DateTimeToFileDate(NewDate);

        FileHandle := FileOpen(FileName, fmOpenReadWrite or fmShareDenyWrite);
        if FileHandle > 0 then
            begin
                FileSetDate(FileHandle, FileDate);
                FileClose(FileHandle);
            end;
    except
        begin
            // ERROR Log
            err.Msg('FileReqThrd.SetFileDate');
        end;
    end;
end;

对于“NewDate”参数,我使用从服务器获取的日期时间。 我尝试像这样从服务器转换日期时间以获得有效的最后写入时间(我正在从 WCF 请求数据,这就是我将其转换为 UTCDateTime 的原因,来自 WCF 服务的未更改数据是 TXSDateTime):

TDateTime cloudFileDateTime := StrToDateTime(DateTimeToStr(cloudDownloadResult.FileCloudData.Lastwritetime.AsUTCDateTime));

但最终我的lastwritetime属性在冬季期间的文件中的lastwritetime是错误的-1h。

我希望您理解我的问题并能给我一个解决方案。

致以诚挚的问候

最佳答案

最简单的方法是调用 TFile.SetLastWriteTimeUtc来自 System.IOUtils 单元。

TFile.SetLastWriteTimeUtc(FileName, 
    DateTimeUtc);

如果此函数不可用,请使用 Win32 API 函数 SetFileTime

在这种情况下,您还需要 DateTimeToSystemTimeSystemTimeToFileTime

关于Delphi:SetFileDate 创建错误的 LastWriteTime(夏季/冬季),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25485998/

相关文章:

delphi - 优化数据集循环

delphi - 软件包的BPL转到错误的软件包输出目录

delphi - OpenGL Delphi中的渲染错误

c# - 创建后无法立即写入文件

Delphi - 使用接口(interface)内的接口(interface)时,我出现内存泄漏,但不知道为什么

javascript-如何使用 moment.js(或替代库)解析日期以选出 2 月 31 日或 6 月 31 日等。无效?

python - 尝试在 Python 3 中将时间转换为整数

php - 使用 DateTime::CreateFromFormat 创建日期对象

Windows 命令行 - 将文件修改时间戳更改为较早的日期时间

Swift3.0 fileAttributes 在现有文件上抛出 "no such file"错误