/// <summary> /// Event handler that decodes DigitalProductId and shows result in ProductKey textbox. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnParseDigitalProductId_Click(object sender, EventArgs e) { // Try to decode input string and detect, if the string is in expected format. // Maybe some regex would do it better? ;) // // let wrap everything we do with input string into try/catch to display error // message when anything fails try { // First try to split the string into the key and value var keyValue = tbDigitalProductId.Text.Trim().Split('='); // Get the key var key = keyValue[0].Trim(); // Is this a valid key if (!key.Equals("\"DigitalProductId\"")) { SetParseErrorMessage(); return; } // Get the value and remove unwanted characters... var value = keyValue[1].Replace("\\\r\n", "").Replace(" ", "").Trim(); // Regedit hex value? if (!value.StartsWith("hex:", StringComparison.InvariantCultureIgnoreCase)) { SetParseErrorMessage(); return; } // Convert comma delimited string hex values into byte array var hexValues = value.Remove(0, 4).Split(','); // crete byte array from hex values var byteValues = hexValues.Select(s => Convert.ToByte(s.ToUpper(), 16)).ToArray(); tbProductKey.Text = KeyDecoder.GetWindowsProductKeyFromDigitalProductId( byteValues, cbDigitalProductIdVersion.SelectedIndex == 0 ? DigitalProductIdVersion.UpToWindows7 : DigitalProductIdVersion.Windows8AndUp); btnCopyToClipboard.Enabled = true; } catch (Exception) { SetParseErrorMessage(); } }