修改系統時間(取得服務器時間,使用SetLocalTime API函數,需要UAC權限)


我的客戶遇到系統時間不對,自己又不會改,於是想到利用服務端時間來修改本地的系統時間。

第一步,把下面xml存成uac.xml文件備用。

復制代碼
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">  
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">  
<security>  
<requestedPrivileges>  
<requestedExecutionLevel level="requireAdministrator" uiAccess="false">  
</requestedExecutionLevel>  
</requestedPrivileges>  
</security>  
</trustInfo>  
<dependency>  
<dependentAssembly>  
<assemblyIdentity type="win32"  
name="Microsoft.Windows.Common-Controls"  
version="6.0.0.0"  
processorArchitecture="x86"  
publicKeyToken="6595b64144ccf1df"  
language="*">  
</assemblyIdentity>  
</dependentAssembly>  
</dependency>  
</assembly> 
復制代碼

接下來,看代碼:

復制代碼
procedure TForm12.SyncLocalDateTime;//同步本地時間
var
  systemtime: Tsystemtime;
  DateTime: TDateTime;
begin
  SetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, 'yyyy-MM-dd');
  DateTime := GetServerDateTime;//取服務器日期
  System.Sysutils.DateTimeToSystemTime(DateTime, systemtime);
  SetLocalTime(systemtime);
end;
復制代碼

需要引用單元:Winapi.Windows

編譯時,使用剛才做的uac.xml文件:

在Delphi 10.3+win10測試通過。

原文地址:https://www.cnblogs.com/stroll/p/6913769.html

https://www.cnblogs.com/kinglandsoft/p/10087576.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM