我正在从我的服务器下载一个文件(我只获取最后写入时间属性的字节和日期时间),下载数据后我在本地计算机上创建一个新文件并希望设置最后写入时间属性。 为此,我使用以下方法:
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
。
在这种情况下,您还需要 DateTimeToSystemTime
和 SystemTimeToFileTime
。
关于Delphi:SetFileDate 创建错误的 LastWriteTime(夏季/冬季),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25485998/