public static void GenerateLicFile(string licFile)
        {
            DateTime    currentDateTime             = DateTime.Now;
            CultureInfo englishCulture              = CultureInfo.GetCultureInfo("en-US");
            string      currentDateTimeInEnUSFormat = currentDateTime.Month + "/" + currentDateTime.Day + "/" + currentDateTime.Year + ' ' + currentDateTime.Hour + ":" + currentDateTime.Minute + ":" + currentDateTime.Second;

            using (Stream stream = File.Open(licFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                BinaryFormatter bformatter     = new BinaryFormatter();
                LicenceManager  licenceManager = new LicenceManager();

                licenceManager.LicenceID          = "";
                licenceManager.LastAccessDate     = currentDateTimeInEnUSFormat;
                licenceManager.InstallationDate   = currentDateTimeInEnUSFormat;
                licenceManager.RegistrationDate   = "";
                licenceManager.ClientCode         = "";
                licenceManager.SerialKey          = "";
                licenceManager.ActivationKey      = "";
                licenceManager.Notes              = "";
                licenceManager.TrialDays          = 30;
                licenceManager.TrialLicences      = 25;
                licenceManager.RegisteredLicences = 0;
                licenceManager.HostSignature      = "SXZ4-737A-27LZ-Z993";

                stream.Position = 0;
                bformatter.Serialize(stream, licenceManager);
                stream.Close();
                stream.Dispose();
            }
        }
        public static bool isValidDataBase(string licenceFilePath, string currentCulture)
        {
            bool returnValue = false;

            try
            {
                if (File.Exists(licenceFilePath))
                {
                    using (Stream stream = File.Open(licenceFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        BinaryFormatter bformatter     = new BinaryFormatter();
                        LicenceManager  licenceManager = (LicenceManager)bformatter.Deserialize(stream);

                        DateTime    currentDateTime = DateTime.Now;
                        CultureInfo englishCulture  = CultureInfo.GetCultureInfo("en-US");

                        string currentDateTimeInEnUSFormat = currentDateTime.Month + "/" + currentDateTime.Day + "/" + currentDateTime.Year + ' ' + currentDateTime.Hour + ":" + currentDateTime.Minute + ":" + currentDateTime.Second;

                        if (!string.IsNullOrEmpty(licenceManager.InstallationDate))
                        {
                            DateTime lastinstalltring = DateTime.Parse(licenceManager.InstallationDate, CultureInfo.InvariantCulture);
                            int      day               = lastinstalltring.Day;
                            int      month             = lastinstalltring.Month;
                            int      year              = lastinstalltring.Year;
                            string   command1          = AppLibrary.Protector.EncodeString((10000 - year) + "#" + (100 - day) + "#" + (50 - month));
                            string   valuefromDatabase = DataManager.Provider.Settings.GetInstalledLicDetails();
                            if (valuefromDatabase == command1)
                            {
                                returnValue = true;
                            }
                        }

                        stream.Position = 0;
                        bformatter.Serialize(stream, licenceManager);
                        stream.Close();
                        stream.Dispose();
                    }
                }
            }
            catch { }

            return(returnValue);
        }
        public static bool IsValidLicence(string licenceFilePath, string systemSignature, string currentCulture, out int errorCode, out int trialLicences, out int registeredLicences, out int trialDays, out double elapsedDays, out string message)
        {
            bool isValidLicence = false;

            trialLicences      = 0;
            registeredLicences = 0;
            trialDays          = 0;
            elapsedDays        = 0;
            errorCode          = 0;
            message            = string.Empty;
            CultureInfo englishCulture = CultureInfo.GetCultureInfo("en-US");

            try
            {
                if (File.Exists(licenceFilePath))
                {
                    using (Stream stream = File.Open(licenceFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        BinaryFormatter bformatter     = new BinaryFormatter();
                        LicenceManager  licenceManager = (LicenceManager)bformatter.Deserialize(stream);

                        string requestCode = GetRequestCode(licenceManager.LicenceID);

                        trialDays          = licenceManager.TrialDays;
                        registeredLicences = licenceManager.RegisteredLicences;
                        trialLicences      = licenceManager.TrialLicences;

                        string lastInstallDate = licenceManager.InstallationDate;

                        DateTime lastinstalltring    = DateTime.Parse(lastInstallDate, CultureInfo.InvariantCulture);
                        DateTime firstLaunchDateTime = Convert.ToDateTime(lastinstalltring, CultureInfo.InvariantCulture);

                        string   lastAccessDate1    = licenceManager.LastAccessDate;
                        DateTime lastacsessstring1  = DateTime.Parse(lastAccessDate1, CultureInfo.InvariantCulture);
                        DateTime lastAccessDateTime = Convert.ToDateTime(lastacsessstring1, CultureInfo.InvariantCulture);

                        DateTime firstLaunchDate = Convert.ToDateTime(firstLaunchDateTime, CultureInfo.GetCultureInfo(currentCulture));
                        DateTime lastAccessDate  = Convert.ToDateTime(lastAccessDateTime, CultureInfo.GetCultureInfo(currentCulture));

                        TimeSpan timeSpan = lastAccessDate.Subtract(firstLaunchDate);

                        elapsedDays = timeSpan.TotalDays;

                        if (string.IsNullOrEmpty(licenceManager.ActivationKey))
                        {
                            if (timeSpan.Days >= licenceManager.TrialDays)
                            {
                                errorCode = 401; // Trial period Expired
                            }
                        }
                        if (licenceManager.HostSignature.IndexOf(systemSignature) >= 0)
                        {
                            isValidLicence = true;
                        }
                        else
                        {
                            errorCode = 1001;                           // System Signature does not match
                            string[] serverIDs = licenceManager.HostSignature.Split(",".ToCharArray());
                            if (licenceManager.RegisteredLicences == 0) //in Trial Mode
                            {
                                if (serverIDs.Length == 1)
                                {
                                    errorCode = 0;
                                    licenceManager.HostSignature += "," + systemSignature;
                                    isValidLicence = true;
                                }
                                else
                                {
                                    isValidLicence = false;    // System Signature does not match
                                }
                            }
                            else  //in registered mode
                            {
                                errorCode      = 2001; // license register for system signature does not match.
                                isValidLicence = false;
                            }
                        }

                        stream.Position = 0;
                        bformatter.Serialize(stream, licenceManager);
                        stream.Close();
                        stream.Dispose();
                    }
                }
                else
                {
                    errorCode = 502;  // Licence File does not exists
                }
            }
            catch (Exception ex)
            {
                isValidLicence = false;
                message        = ex.Message;
                errorCode      = 500; // Failed to process Licence file
            }

            return(isValidLicence);
        }
        public static string UpdateLastAccessDateTime(string licenceFilePath, string currentCulture, string systemSignature)
        {
            string returnValue = null;

            try
            {
                if (File.Exists(licenceFilePath))
                {
                    using (Stream stream = File.Open(licenceFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        BinaryFormatter bformatter     = new BinaryFormatter();
                        LicenceManager  licenceManager = (LicenceManager)bformatter.Deserialize(stream);

                        DateTime    currentDateTime = DateTime.Now;
                        CultureInfo englishCulture  = CultureInfo.GetCultureInfo("en-US");

                        string currentDateTimeInEnUSFormat = currentDateTime.Month + "/" + currentDateTime.Day + "/" + currentDateTime.Year + ' ' + currentDateTime.Hour + ":" + currentDateTime.Minute + ":" + currentDateTime.Second;
                        //string currentDateTimeInEnUSFormat = currentDateTime.ToLongDateString() + " - " + currentDateTime.ToLongTimeString();

                        if (string.IsNullOrEmpty(licenceManager.HostSignature))
                        {
                            licenceManager.InstallationDate   = currentDateTimeInEnUSFormat;
                            licenceManager.LastAccessDate     = currentDateTimeInEnUSFormat;
                            licenceManager.TrialDays          = licenceManager.TrialDays;
                            licenceManager.TrialLicences      = licenceManager.TrialLicences;
                            licenceManager.ActivationKey      = null;
                            licenceManager.SerialKey          = null;
                            licenceManager.RegistrationDate   = null;
                            licenceManager.LicenceID          = GetRequestCode("");
                            licenceManager.Notes              = "Application First launched on " + currentDateTimeInEnUSFormat + "<br />";
                            licenceManager.RegisteredLicences = 0;
                            licenceManager.HostSignature      = systemSignature;

                            returnValue = licenceManager.LicenceID;

                            // UpdateDataBase(currentDateTime.Day, currentDateTime.Month, currentDateTime.Year);
                        }

                        //else if (!string.IsNullOrEmpty(licenceManager.HostSignature) &&string.IsNullOrEmpty(licenceManager.InstallationDate))
                        //{
                        //    licenceManager.InstallationDate = currentDateTimeInEnUSFormat;
                        //    licenceManager.LastAccessDate = currentDateTimeInEnUSFormat;
                        //    licenceManager.TrialDays = licenceManager.TrialDays;
                        //    licenceManager.TrialLicences = licenceManager.TrialLicences;
                        //    licenceManager.ActivationKey = null;
                        //    licenceManager.SerialKey = null;
                        //    licenceManager.RegistrationDate = null;
                        //    licenceManager.LicenceID = GetRequestCode("");
                        //    licenceManager.Notes = "Application First launched on " + currentDateTimeInEnUSFormat + "<br />";
                        //    licenceManager.RegisteredLicences = 0;


                        //    returnValue = licenceManager.HostSignature;
                        //}
                        else if (!string.IsNullOrEmpty(licenceManager.LastAccessDate))
                        {
                            licenceManager.TrialDays = 615;

                            string lastAccessDate = licenceManager.LastAccessDate;
                            returnValue = licenceManager.LicenceID;
                            DateTime lastacsessstring   = DateTime.Parse(lastAccessDate, CultureInfo.InvariantCulture);
                            DateTime lastAccessDateTime = Convert.ToDateTime(lastacsessstring, CultureInfo.InvariantCulture);
                            DateTime dtLastAceess       = Convert.ToDateTime(lastAccessDateTime, CultureInfo.GetCultureInfo(currentCulture));

                            TimeSpan ts = DateTime.Now.Subtract(dtLastAceess);

                            if (ts.TotalDays > 0)
                            {
                                licenceManager.LastAccessDate = currentDateTimeInEnUSFormat;
                            }


                            DateTime installDate = DateTime.Parse(licenceManager.LastAccessDate, CultureInfo.InvariantCulture);
                            // UpdateDataBase(installDate.Day, installDate.Month, installDate.Year);
                        }



                        stream.Position = 0;
                        bformatter.Serialize(stream, licenceManager);
                        stream.Close();
                        stream.Dispose();
                    }
                }
            }
            catch (Exception)
            {
                returnValue = null;
            }
            return(returnValue);
        }