private void cmdOK_Click(object sender, EventArgs e)
 {
     if (!ProductKey.IsValidKey(txtProductKey.Text, txtID.Text))
     {
         MessageBox.Show("Invalid Product Key.", "Invalid");
         return;
     }
     else
     {
         ProductKey pk          = new ProductKey(txtProductKey.Text, txtID.Text);
         String     failmessage = "(Unspecified reason)";
         if (additionalchecker != null)
         {
             if (!String.IsNullOrEmpty(failmessage = additionalchecker(pk)))
             {
                 MessageBox.Show("Invalid Product Key - " + failmessage, "Invalid");
                 return;
             }
         }
         //save the productkey.
         pk.Register(_ProductName);
         constructedPK = pk;
         Close();
     }
 }
 /// <summary>
 /// Determines if this Product Key is valid for the given machineID.
 /// </summary>
 /// <param name="ProductKey">Product Key to test</param>
 /// <returns>true if key given is valid for the given MachineID; false otherwise.</returns>
 public static bool IsValidKey(String ProductKey, String MachineID)
 {
     //simply try to construct a FeatureLicenseData Object. it will throw an exception
     //from it's constructor if the given values aren't valid.
     try
     {
         ProductKey fld = new ProductKey(ProductKey, MachineID);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        /// <summary>
        /// retrieves the ProductKey for the given product from the registry.
        /// </summary>
        /// <param name="ProductName">Name of product to retrieve.</param>
        /// <returns>the ProductKey Object for the given product, or null if the application is not registered.</returns>
        public static ProductKey GetRegisteredProduct(String ProductName, String IDString)
        {
            //step one, get the ProductCode.
            try
            {
                RegistryKey rk      = Registry.CurrentUser.CreateSubKey(Licensingregkey);
                RegistryKey appkey  = rk.CreateSubKey(ProductName);
                String      getcode = (String)appkey.GetValue("Key");
                String      getID   = (String)appkey.GetValue("ID");

                ProductKey retrievekey = new ProductKey(getcode, getID);
                return(retrievekey);
            }
            catch (Exception exx)
            {
                return(null);
            }
        }
        /// <summary>
        /// GetProductInformation: retrieves the product information from the registry. Or, if request is true, will
        /// show the registration dialog and request the information for a key.
        /// </summary>
        /// <param name="AppName">Name of Application. This will be used for looking up the Application name, and should match a previous call to Register.</param>
        /// <param name="AdditionalCheck">Additional Function to perform further checks.</param>
        /// <param name="request">whether to show the dialog and request product key information if there is none accessible.</param>
        /// <param name="ownerwindow">Owner window for any dialogs.</param>
        /// <returns>a ProductKey Object representing valid ProductKey information for the given product if successful. null if the request dialog is cancelled or if request is false and
        /// no information is found for the given product.</returns>
        /// <remarks>This function will also call Register() if request is true and it needs to show the dialog.</remarks>
        public static ProductKey GetProductInformation(String AppName, frmProductReg.AdditionalValidCheckFunction AdditionalCheck, bool request, IWin32Window ownerwindow, String IDString)
        {
            ProductKey pk = GetRegisteredProduct(AppName, IDString);

            if (pk == null && request)
            {
                if (String.IsNullOrEmpty(IDString))
                {
                    IDString = ProductKey.getLocalMachineID();
                }
                //if request is enabled, we need to show the dialog.
                frmProductReg regdialog = new frmProductReg(AppName, AdditionalCheck, IDString);

                if (regdialog.ShowDialog() == DialogResult.Cancel)
                {
                    return(null);
                }

                //otherwise, return the result.
                return(regdialog.constructedPK);
            }

            return(pk);
        }
 private String EnsureProductRoutine(ProductKey pk)
 {
     return(pk.Product == ProductType ? "" : "Product Key not for this Product");
 }