using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace InstaSoftOfficeTool.Services { public class LicenseManager { public string OsppPath { get; private set; } public bool FindOspp() { var searchPaths = new[] { // Click-to-Run (leggyakoribb — Office 365, 2019, 2021, 2024) @"C:\Program Files\Microsoft Office\root\Office16\OSPP.VBS", @"C:\Program Files (x86)\Microsoft Office\root\Office16\OSPP.VBS", // Hagyományos MSI @"C:\Program Files\Microsoft Office\Office16\ospp.vbs", @"C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs", @"C:\Program Files\Microsoft Office\Office15\ospp.vbs", @"C:\Program Files (x86)\Microsoft Office\Office15\ospp.vbs", @"C:\Program Files\Microsoft Office\Office14\ospp.vbs", @"C:\Program Files (x86)\Microsoft Office\Office14\ospp.vbs", }; foreach (var path in searchPaths) { if (File.Exists(path)) { OsppPath = path; return true; } } try { var c2rPath = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\Office\ClickToRun\Configuration") ?.GetValue("InstallationPath") as string; if (!string.IsNullOrEmpty(c2rPath)) { // C2R: root\Office16 vagy sima Office16 var candidates = new[] { Path.Combine(c2rPath, "root", "Office16", "OSPP.VBS"), Path.Combine(c2rPath, "Office16", "OSPP.VBS"), }; foreach (var candidate in candidates) { if (File.Exists(candidate)) { OsppPath = candidate; return true; } } } } catch { } return false; } public async Task GetStatusAsync() { if (string.IsNullOrEmpty(OsppPath)) return "Az ospp.vbs nem tal\u00e1lhat\u00f3."; var runner = new ProcessRunner(); return await runner.RunAndCaptureAsync("cscript", "//Nologo \"" + OsppPath + "\" /dstatus"); } public List ParseLicenseKeys(string dstatusOutput) { var keys = new List(); var regex = new Regex(@"Last 5 characters of installed product key:\s*(\S+)", RegexOptions.IgnoreCase); foreach (Match match in regex.Matches(dstatusOutput)) { keys.Add(match.Groups[1].Value); } return keys; } public async Task RemoveKeyAsync(string last5Chars) { if (string.IsNullOrEmpty(OsppPath)) return "Az ospp.vbs nem tal\u00e1lhat\u00f3."; var runner = new ProcessRunner(); return await runner.RunAndCaptureAsync("cscript", "//Nologo \"" + OsppPath + "\" /unpkey:" + last5Chars); } public async Task RemoveAllKeysAsync() { var status = await GetStatusAsync(); var keys = ParseLicenseKeys(status); if (keys.Count == 0) return "Nem tal\u00e1lhat\u00f3 telep\u00edtett term\u00e9kkulcs."; var results = new List(); foreach (var key in keys) { var result = await RemoveKeyAsync(key); results.Add(key + ": " + result.Trim()); } return string.Join("\n", results); } } }