In C#
, I'm using Process
and command line
to check the license status of MS Office
.
private bool isLicensed(){
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/K";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WorkingDirectory = officeDirectory; // \Program Files\Microsoft Office\Office16 or \Program Files (x86)\Microsoft Office\Office16
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("cscript ospp.vbs /dstatus");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
string response = process.StandardOutput.ReadToEnd();
if (response.ToUpperInvariant().Contains("LICENSE STATUS: ---LICENSED---"))
{
return true;
}
}
Here is the example result of using the command cscript ospp.vbs /dstatus
.
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
---Processing--------------------------
---------------------------------------
PRODUCT ID: 00202-51649-34724-AA786
SKU ID: 149dbce7-a48e-44db-8364-a53386cd4580
LICENSE NAME: Office 16, Office16O365ProPlusR_Subscription1 edition
LICENSE DESCRIPTION: Office 16, TIMEBASED_SUB channel
BETA EXPIRATION: 01/01/1601
LICENSE STATUS: ---LICENSED---
ERROR CODE: 0x4004FC04 (for information purposes only as the status is licensed)
ERROR DESCRIPTION: The Software Licensing Service reported that the application is running within the timebased validity period.
REMAINING GRACE: 17 days (25226 minute(s) before expiring)
Last 5 characters of installed product key: CJ9C2
---------------------------------------
PRODUCT ID: 00411-80000-00000-AA917
SKU ID: 7d351d13-e2f4-49ea-a63c-63a85b1d5630
LICENSE NAME: Office 19, Office19ProjectPro2019R_Grace edition
LICENSE DESCRIPTION: Office 19, RETAIL(Grace) channel
BETA EXPIRATION: 01/01/1601
LICENSE STATUS: ---OOB_GRACE---
ERROR CODE: 0x4004F00C
ERROR DESCRIPTION: The Software Licensing Service reported that the application is running within the valid grace period.
REMAINING GRACE: 4 days (5819 minute(s) before expiring)
Last 5 characters of installed product key: B4JJJ
---------------------------------------
PRODUCT ID: 00417-40000-00000-AA421
SKU ID: db2760f6-f36b-403c-a0e7-0b59cde7427b
LICENSE NAME: Office 19, Office19VisioPro2019R_Grace edition
LICENSE DESCRIPTION: Office 19, RETAIL(Grace) channel
BETA EXPIRATION: 01/01/1601
LICENSE STATUS: ---OOB_GRACE---
ERROR CODE: 0x4004F00C
ERROR DESCRIPTION: The Software Licensing Service reported that the application is running within the valid grace period.
REMAINING GRACE: 4 days (7113 minute(s) before expiring)
Last 5 characters of installed product key: 684YW
---------------------------------------
---------------------------------------
---Exiting-----------------------------
But this will always return True
if there are existing MS Office, MS Project, MS Visio and one of them is licensed. How can I check the licensed status one at a time?
User contributions licensed under CC BY-SA 3.0