This might sound like a little bit of a crazy question, but how can I find out (hopefully via an API/registry key) the install time and date of Windows?
The best I c
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallDate and systeminfo.exe produces the wrong date.
The definition of UNIX timestamp is timezone independent. The UNIX timestamp is defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970 and not counting leap seconds.
In other words, if you have installed you computer in Seattle, WA and moved to New York,NY the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallDate will not reflect this. It's the wrong date, it doesn't store timezone where the computer was initially installed.
The effect of this is, if you change the timezone while running this program, the date will be wrong. You have to re-run the executable, in order for it to account for the timezone change.
But you can get the timezone info from the WMI Win32_Registry class.
InstallDate is in the UTC format (yyyymmddHHMMSS.xxxxxx±UUU) as per Microsoft TechNet article "Working with Dates and Times using WMI" where notably xxxxxx is milliseconds and ±UUU is number of minutes different from Greenwich Mean Time.
private static string RegistryInstallDate()
{
DateTime InstallDate = new DateTime(1970, 1, 1, 0, 0, 0); //NOT a unix timestamp 99% of online solutions incorrect identify this as!!!!
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Registry");
foreach (ManagementObject wmi_Windows in searcher.Get())
{
try
{
///CultureInfo ci = CultureInfo.InvariantCulture;
string installdate = wmi_Windows["InstallDate"].ToString();
//InstallDate is in the UTC format (yyyymmddHHMMSS.xxxxxx±UUU) where critically
//
// xxxxxx is milliseconds and
// ±UUU is number of minutes different from Greenwich Mean Time.
if (installdate.Length==25)
{
string yyyymmddHHMMSS = installdate.Split('.')[0];
string xxxxxxsUUU = installdate.Split('.')[1]; //±=s for sign
int year = int.Parse(yyyymmddHHMMSS.Substring(0, 4));
int month = int.Parse(yyyymmddHHMMSS.Substring(4, 2));
int date = int.Parse(yyyymmddHHMMSS.Substring(4 + 2, 2));
int hour = int.Parse(yyyymmddHHMMSS.Substring(4 + 2 + 2, 2));
int mins = int.Parse(yyyymmddHHMMSS.Substring(4 + 2 + 2 + 2, 2));
int secs = int.Parse(yyyymmddHHMMSS.Substring(4 + 2 + 2 + 2 + 2, 2));
int msecs = int.Parse(xxxxxxsUUU.Substring(0, 6));
double UTCoffsetinMins = double.Parse(xxxxxxsUUU.Substring(6, 4));
TimeSpan UTCoffset = TimeSpan.FromMinutes(UTCoffsetinMins);
InstallDate = new DateTime(year, month, date, hour, mins, secs, msecs) + UTCoffset;
}
break;
}
catch (Exception)
{
InstallDate = DateTime.Now;
}
}
return String.Format("{0:ffffd d-MMM-yyyy h:mm:ss tt}", InstallDate);
}