0

I am able to read the start time from my Win7 computer.

public static DateTime WindowsStartTime()
{
    DateTime dt = new DateTime();

    try
    {
        dt = DateTime.Now - new TimeSpan(0, 0, 0, 0, System.Environment.TickCount);
        return dt;
    }
    catch (Exception ex)
    {
        return dt;
    }
}

Unfortunately, this code is not working on Win10 computer. Does someone have an idea why this is not working ?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Ariwa
  • 31
  • 8

1 Answers1

2

Well, since System.Environment.TickCount is of type int it has maximum value

 int.MaxValue = 2147483647 // milliseconds

which is

 2147483647 ms = 
 2147483.647 seconds ~ 
 24.85 days

so if you switched on your workstation about a month ago or earlier you'll get wrong time because of integer overflow (not Windows version)

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • if that is the issue kudos for thinking outside the box! Also, some people dont turn off their pc for over a month??? – Denis Schaf Mar 14 '19 at 13:25
  • @DenisSchaf This behavior is well explained in the Remarks section of the [Environment.​Tick​Count Property](https://learn.microsoft.com/en-us/dotnet/api/system.environment.tickcount?view=netframework-4.7.2) page. – Clemens Mar 14 '19 at 13:32
  • i still wouldnt have thought of it, as the question did not define "this code is not working" @Clemens – Denis Schaf Mar 14 '19 at 13:41