Makanar Makanar - 3 years ago 167
C# Question

Get date computer was first used

I am trying to get how many days ago Store-bought HP or Dell Windows 10 PC was first used, using c#/windows forms. What I mean by this is the date the computer went through the intial setup, such as creating a Username and password, etc.

Getting the install date from HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallDate will not work, as features updates change that date.

My hope is that there is a folder or file that is created when the PC is first setup/used, and is not changed even with feature updates like the Creator's Update, anniversary update, etc.

Does any know of such a folder or file, or another way to get some info like this through C#?

Edit: These suggested folders/files creations dates are lost with creators update on the Virtual machine I am using or are older then install.
C:\Windows\system.ini
C:\Windows\win.ini
C:\Users\User
C:\bootmgr
C:\bootnxt
C:\$Recycle.Bin

Answer Source

Update I modified the answer to loop through all special folders, since I found some that were older than the oldest user folder

This is not at all guaranteed, hopefully someone can tell me if it's wrong and I'll delete it, but one idea is to get the oldest creation date of the SpecialFolder objects:

static void Main()
{
    // This will hold the oldest date, so start it at MaxValue
    var oldestDate = DateTime.MaxValue;

    // Loop through each defined special folder
    foreach (Environment.SpecialFolder specialFolder in 
        Enum.GetValues(typeof(Environment.SpecialFolder)))
    {
        // Get the path to this folder
        var folderPath = Environment.GetFolderPath(specialFolder);

        // Some special folders may not exist, so verify the path first
        if (Directory.Exists(folderPath))
        {
            // If the created date of this folder is older, update our variable
            var createDate = Directory.GetCreationTime(folderPath);
            if (createDate < oldestDate) oldestDate = createDate;
        }
    }

    Console.WriteLine($"The oldest speical folder was created on: {oldestDate}");

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

Update 2

Getting the creation of the windows root directory also gives the oldest time (at least for me). Possibly this is simpler?

var createDate = Directory.GetCreationTime(Path.GetPathRoot(
    Environment.GetFolderPath(Environment.SpecialFolder.Windows)));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download