c# - 应用程序在重新启动之前看不到系统时区更改

标签 c# windows datetime time timezone

<分区>

我觉得这是一个非常简单的问题,以前肯定有人遇到过这个问题。要么我想不出正确的搜索关键字,要么没有人真正遇到过这个问题。

我的问题是,当我进入 Windows 系统时间并更改时区时,系统托盘时间会相应更新,但我正在运行的应用程序似乎完全没有注意到这一事实。我尝试过的所有 .NET 对象似乎都没有反射(reflect)系统时区已更改的事实。我在之前和之后检查了以下对象,看看它们是否发生了变化:

DateTime.Now;
DateTime.Now.Offset;
TimeZone.CurrentTimeZone.StandardName;

对我来说,时区会在应用程序的同一电源循环中发生变化,这对我来说是一个非常小的案例,但无论如何,我必须解决它。有没有办法告诉 .NET 框架“刷新您的时区”或其他什么?

最佳答案

查看该属性的文档:

http://msdn.microsoft.com/en-us/library/system.timezone.currenttimezone(v=vs.110).aspx

Local time zone data is cached after CurrentTimeZone is first used to retrieve time zone information. If the system's local time zone subsequently changes, the CurrentTimeZone property does not reflect this change. If you need to handle time zone changes while your application is running, use the TimeZoneInfo class and call its TimeZoneInfo.ClearCachedData method.

TimeZoneInfo local = TimeZoneInfo.Local;

TimeZoneInfo.ClearCachedData();
local = TimeZoneInfo.Local; //updated local time zone info

关于c# - 应用程序在重新启动之前看不到系统时区更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23393730/

相关文章:

windows - 不允许使用 SharePoint token 类型

javascript - 如何创建一个可以在javascript中返回日期差异的函数?

c# - 动态访问表单元素

c# - 如何在 C# 中使用 ref 属性

javascript - iframe点击使div显示隐藏

php - 将 TIMESTAMP 和时区转换为不同的时区

python - 从 2000 年 1 月 1 日开始从 UNIX 时间转换为时间戳

c# - ASP 从 C# 调用 javaScript

windows - 查找调用 Windows 程序的快捷方式

c++ - 为 Windows 10 64 位安装的 OpenCV3.2.0 中缺少许多 .lib 文件