Get Windows Installation Date in C# or VB.NET
A while back, one of the guys at work asked me if it was possible to determine the Windows installation date of some computers. So after a short bit of digging online, I found that the registry already contains a value that indicates the number of seconds after January 1, 1970 @ 12:00 am that the operating system was installed. I just wanted to share it with everyone just in case someone may need this.
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static DateTime GetWindowsInstallationDateTime(string computerName) { Microsoft.Win32.RegistryKey key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, computerName); key = key.OpenSubKey(@"SOFTWAREMicrosoftWindows NTCurrentVersion", false); if (key != null) { DateTime startDate = new DateTime(1970, 1, 1, 0, 0, 0); Int64 regVal = Convert.ToInt64(key.GetValue("InstallDate").ToString()); DateTime installDate = startDate.AddSeconds(regVal); return installDate; } return DateTime.MinValue; } |
VB.NET
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Public Shared Function GetWindowsInstallationDateTime(computerName as String) as DateTime Dim key as Microsoft.Win32.RegistryKey key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, computerName) key = key.OpenSubKey("SOFTWAREMicrosoftWindows NTCurrentVersion", False) If key IsNot Nothing Then Dim startDate as DateTime Dim regVal as Int64 startDate = new DateTime(1970, 1, 1, 0, 0, 0) regVal = Convert.ToInt64(key.GetValue("InstallDate").ToString()) Return startDate.AddSeconds(regVal) End If Return DateTime.MinValue End Function |
Thank you. I was looking for this 😉