Crash when accessing DateTime.Now

Viewed 857

The following c# code runs OK as a console app on a Win 10 dev box when built for .Net 4.5:

static void Main(string[] args)
    {
        try
        {
            Console.Write(DateTime.Now.ToString());
        }
        catch (Exception Ex)
        {
            Console.Write(Ex.Message);
        }

    }

But when run on a Win 7 box with .Net 4.6.1 it has started crashing showing the following message:

    Unhandled Exception: System.Runtime.InteropServices.SEHException: External compo
    nent has thrown an exception.
       at System.TimeZoneInfo.TransitionTime.Equals(TransitionTime other)
       at System.TimeZoneInfo.CreateAdjustmentRuleFromTimeZoneInformation(RegistryTi
    meZoneInformation timeZoneInformation, DateTime startDate, DateTime endDate, Int
    32 defaultBaseUtcOffset)
       at System.TimeZoneInfo..ctor(TimeZoneInformation zone, Boolean dstDisabled)
       at System.TimeZoneInfo.GetLocalTimeZoneFromWin32Data(TimeZoneInformation time
    ZoneInformation, Boolean dstDisabled)
       at System.TimeZoneInfo.CachedData.GetCurrentOneYearLocal()
       at System.TimeZoneInfo.CachedData.GetOneYearLocalFromUtc(Int32 year)
       at System.TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(DateTime time, Boolean&
    isAmbiguousLocalDst)
       at System.DateTime.get_Now()
       at DateCrashTest.Program.Main(String[] args) in c:\Users\Simon\Documents\Visu
    al Studio 2013\Projects\DateCrashTest\DateCrashTest\DateCrashTest\Program.cs:lin
    e 15

I have two questions:

1) Why is the exception not trapped?

2) Why does the program run OK on a different Win7 box with same .Net Framework and in the same TimeZone? I have checked the .Net Framework Install on the problem box and it appears to be OK. The problem has appeared recently - it did not occur a few days ago. Rebooting has not helped.

1 Answers

The problem is not in your code. Most likely there is something wrong with that particular Windows 7 instance. As others already suggested you could run sfc /scannow, but it could be also corrupted registry or something else.

I would suggest to reinstall the machine as it's the simplest and the fastest solution to this problem.

Related