registry value returns 0

Ripsha

New member
Joined
Nov 25, 2015
Messages
1
Programming Experience
Beginner
Good afternoon,

I'm new to c# and pulled a code yesterday to convert the Original Install Date for my operating system from the registry D Word to a DateTime output. Now the code does do what it's supposed to do except display the actual date (instead shows the default date of 1/1/1970). There is a snippet in the code that should convert the D Word (which is the seconds after that default date as designated by Microsoft) to give you the actual OS install date but it does not. This is the code as follows:

C#:
        public static DateTime GetWindowsInstallationDateTime(string scomputer)        {
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, scomputer);
            key = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", 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;
            
        }

and this is how I call it into the application:

string scomputer = System.Environment.MachineName;
SosDate.Text = GetWindowsInstallationDateTime(scomputer).ToString();

Any help is appreciated!
 
Specify view parameter for OpenRemoteBaseKey call, and try RegistryView.Registry32 or RegistryView.Registry64. Default is the 32/64bit mode application is running under, if you ask for value for wrong view you only get 0 returned.

You also need to run the application as administrator (elevated).

Also remember to close/dispose any RegistryKey object that you open.
 
Back
Top Bottom