当前位置: 首页 > news >正文

C# 22H2之后的windows版本使用SetDynamicTimeZoneInformation设置时区失败处理

使用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 = SetTimeZoneByPowerShell(dynamicTimeZoneInformation.timeZoneKeyName);}return success;
}/// <summary>
/// 根据时区名获取对应的DynamicTimeZoneInformation
/// </summary>
/// <param name="timeZoneName"></param>
/// <returns></returns>
private static DynamicTimeZoneInformation ConvertDynamicTimeZoneInformation(string timeZoneName)
{var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);var info = new DynamicTimeZoneInformation{standardName = timeZoneInfo.StandardName,standardDate = new SystemTime(),daylightName = timeZoneInfo.DaylightName,daylightDate = new SystemTime(),timeZoneKeyName = timeZoneInfo.Id,dynamicDaylightTimeDisabled = false,bias = -Convert.ToInt32(timeZoneInfo.BaseUtcOffset.TotalMinutes)};return info;
}/// <summary>
/// 判断Windows系统是否为旧版本
/// </summary>
/// <returns></returns>
public static bool IsOldOsVersion()
{var os = Environment.OSVersion;return os.Platform != PlatformID.Win32NT || os.Version.Major < 6;
}/// <summary>
/// 将DynamicTimeZoneInformation转换为TimeZoneInformation
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private static TimeZoneInformation ConvertTimeZoneInformation(DynamicTimeZoneInformation info)
{return new TimeZoneInformation{bias = info.bias,standardName = info.standardName,standardDate = info.standardDate,standardBias = info.standardBias,daylightName = info.daylightName,daylightDate = info.daylightDate,daylightBias = info.daylightBias};
}// 针对于旧Windows系统,如Windows XP
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetTimeZoneInformation(ref TimeZoneInformation lpTimeZoneInformation);[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation(ref TimeZoneInformation lpTimeZoneInformation);//针对于新Windows系统,如Windows 7,Windows 8, Windows10
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetDynamicTimeZoneInformation(ref DynamicTimeZoneInformation lpDynamicTimeZoneInformation);[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetDynamicTimeZoneInformation(ref DynamicTimeZoneInformation lpDynamicTimeZoneInformation);/// <summary>
/// 通过PowerShell设置时区
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool SetTimeZoneByPowerShell(string id)
{using var powerShell = PowerShell.Create();try{//通过PowerShell设置时区powerShell.AddCommand("Set-TimeZone").AddParameter("Id", id);powerShell.Invoke();}catch (Exception){return false;}return true;}
http://www.lryc.cn/news/237987.html

相关文章:

  • 分布式与微服务 —— 初始
  • 多因素方差分析(Multi-way Analysis of Variance) R实现
  • git撤销某一次commit提交
  • 数据结构详细笔记——图
  • 黑马React18: 基础Part II
  • Maven工程继承关系,多个模块要使用同一个框架,它们应该是同一个版本,项目中使用的框架版本需要统一管理。
  • Selenium UI 自动化
  • 竞赛 题目:基于深度学习的图像风格迁移 - [ 卷积神经网络 机器视觉 ]
  • 【unity3D-网格编程】01:Mesh基础属性以及用代码创建一个三角形
  • Java贪吃蛇小游戏
  • Linux:系统基本信息扫描(1)
  • VR全景打造亮眼吸睛创意内容:三维模型、实景建模
  • ProTable高级表格获取表单数据
  • 力扣刷题第二十七天--二叉树
  • 一个快递包裹的跨国之旅
  • qsort函数使用方法总结
  • 机器学习介绍与分类
  • linux控制台命令
  • 快时尚品牌Halara登上TikTok美国小店榜Top 5,运动健身风靡TikTok
  • Docker 安装 Oracle Database 23c
  • 什么是美国服务器,有哪些优势,适用于什么场景?
  • TeXLive 2023安装教程
  • uniapp中swiper 轮播带左右箭头,点击切换轮播效果demo(整理)
  • 网络连接Android设备
  • Redis(位图Bitmap和位域Bitfield)
  • 【ArcGIS】批量对栅格图像按要素掩膜提取
  • 二进制安装minio 并实现主从同步
  • React中封装echarts图表组件以及自适应窗口变化
  • 鸿蒙:使用Stack、ContentTable、Flex等组件和布局实现一个显示界面
  • 3.生成验证码 + 开发登录、退出功能 + 显示登录信息