AxelS
AxelS

Reputation: 329

Get Offline Installation ID (Windows 8 or similar)

Is it possible to get the Windows Offline Installation ID (OfflineInstallationId was found in slmgr.vbs)?

This can be achieved on the command-line with:

slmgr.vbs /dti

This returns the Installation ID without spaces or dashes, is it possible to retrieve this in C#? I want to eventually have each block of the ID (there are 9 blocks, 7 numbers in each block) in their own textboxes on a form.

There are 9 blocks, each with 7 numbers

I looked around, but couldn't find any examples for C#.

Upvotes: 0

Views: 3390

Answers (2)

zhzhwcn
zhzhwcn

Reputation: 85

Notice, you should also check the Name Or ApplicationID. If you installed office, you will get two or more OfflineInstallationIds.

Check the Name contains Windows or check the ApplicationID equals 55c92734-d682-4d71-983e-d6ec3f16059f

PS: I dont know if the AppliactionID changes between diffrent version of Windows. I did some Google search and found it seems a static value.

Upvotes: 0

AStopher
AStopher

Reputation: 4550

This is possible by querying the WMI (Windows Management Instrumentation)'s Win32_WindowsProductActivation (XP and below) or SoftwareLicensingProduct (Vista or higher) class:

Requires these namespaces to be declared:

  • System
  • System.Collections.Generic
  • System.Management
  • System.Text

Declare those namespaces at the top of your codefile with using, as per se:

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

then use the following code in a function:

ManagementScope Scope;
Scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
Scope.Connect();
ObjectQuery Query = new ObjectQuery("SELECT OfflineInstallationId FROM SoftwareLicensingProduct");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
    //Do whatever with the Offline Installation ID here.   
}

I noticed that because I used Windows 8.1's 'refresh' feature once, my WMI returned two Offline Installation IDs for me, therefore this should be something you need to consider (the first Offline Installation ID is the valid one):

enter image description here

You may want something similar to this:

static string getOfflineInstallId()
{
    ManagementScope Scope;
    Scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
    Scope.Connect();
    ObjectQuery Query = new ObjectQuery("SELECT OfflineInstallationId FROM SoftwareLicensingProduct");
    ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

    foreach (ManagementObject WmiObject in Searcher.Get())
    {
        if (WmiObject["OfflineInstallationId"] != null)
            return WmiObject["OfflineInstallationId"].ToString();
    }
    return ""; //Making the compiler happy.
}

As mentioned earlier, SoftwareLicensingProduct with OfflineInstallationId only works on Windows versions newer than Vista (e.g, Vista, 7, 8, 8.1, and 10), therefore if you (or the users of your program) wish to use it on XP or earlier, you may be able to use the following, but however as I do not have access to XP I am unable to test this:

ObjectQuery Query = new ObjectQuery("SELECT GetInstallationID FROM Win32_WindowsProductActivation");

If you want to separate each block of the ID and insert into a TextBox into a Windows Form (WinForm), you can use the below code:

string installId = getOfflineInstallId();
StringBuilder sb = new StringBuilder();
bool fRun = false;
for (int i = 0; i < installId.Length; i++)
{
    if (i % 7 == 0)
    {
        if (fRun)
            sb.Append('-');
        else
            fRun = true; //Stops a '-' being added at the 1st position.
    }
    sb.Append(installId[i]);
}
idTextBox.Text = sb.ToString();

Something like this:

enter image description here

Further to this, you'll also need to reference the System.Management libraries by right-clicking on References in Solution Explorer, clicking on Add New Reference, and adding System.Management and System.Management.Instrumentation to the project:

enter image description here

The project used in this example can be downloaded here (requires Visual Studio 2013 or higher).

Upvotes: 6

Related Questions