Domanda

I feel this is a pretty simple question and somebody must have had this problem before. Either I can't come up with the correct search key words or nobody really has had this problem.

My problem is when I go into the windows system time and change the time zone, the system tray time updates accordingly, but my running application appears to be completely oblivious of this fact. None of the .NET objects I've tried seem to reflect the fact that the system timezone has been changed. I've checked the following objects, before and after, to see if they change and they do not:

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

This is a pretty fringe case for me that the time zone would change in the same power cycle of the application, but nevertheless, I have to resolve it. Is there a way to tell the .NET framework to "refresh your time zone" or something?

È stato utile?

Soluzione

Take a look at the documentation for that property:

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top