使用SetDynamicTimeZoneInformation设置时区返回false,设置失败。
使用PowerShell设置Set-TimeZone成功。
/// <summary>
/// 设置本地时区
/// 参数取值"China Standard Time",即可设置为中国时区
/// </summary>
/// <param name="timeZoneId"></param>
/// <returns></returns>
public static bool SetLocalTimeZone(string timeZoneId)
{
var dynamicTimeZoneInformation = ConvertDynamicTimeZoneInformation(timeZoneId);
bool success;
// 检测当前系统是否为旧系统
if (IsOldOsVersion())
{
var tzi = ConvertTimeZoneInformation(dynamicTimeZoneInformation);
success = SetTimeZoneInformation(ref tzi);
}
else
{
success = SetDynamicTimeZoneInformation(ref dynamicTimeZoneInformation);
}
if (success)
{
TimeZoneInfo.ClearCachedData(); // 清除缓存
}
else
{
success = SetTimeZoneByPowerShe

本文介绍了如何在不同版本的Windows系统中使用SetDynamicTimeZoneInformation和PowerShellAPI来设置本地时区,特别关注了对旧系统(如WindowsXP)的兼容处理。
最低0.47元/天 解锁文章
852

被折叠的 条评论
为什么被折叠?



