123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using System;
- using System.Collections.Generic;
- namespace Vinno.vCloud.Common.License.Licenses
- {
- public static class LicenseFileHelper
- {
- /// <summary>
- /// used'$'when saving File
- /// </summary>
- private static char separator = '$';
- /// <summary>
- /// Get text from Lincese
- /// </summary>
- /// <param name="lincese"></param>
- /// <returns></returns>
- public static string LinceseToFile(License lincese)
- {
- string text = string.Empty;
- string expirationUtcDatetext = "ExpirationUtcDate=" + lincese.ExpirationUtcDate;
- text += expirationUtcDatetext + separator;
- string maxAccountCounttext = "MaxAccountCount=" + lincese.MaxAccountCount;
- text += maxAccountCounttext + separator;
- string maxUsCounttext = "MaxUsCount=" + lincese.MaxUsCount;
- text += maxUsCounttext + separator;
- string maxDiskSpacetext = "MaxDiskSpace=" + lincese.MaxDiskSpace;
- text += maxDiskSpacetext + separator;
- string idtext = "Identification=" + lincese.Identification;
- text += idtext + separator;
- string funItemValue = "FunctionalItemValue=" + lincese.FunctionalItemValue;
- text += funItemValue + separator;
- string asskey = "AssociationKey=" + lincese.AssociationKey;
- text += asskey + separator;
- string useVersionValue = "LicenseType=" + lincese.LicenseType.ToString();
- text += useVersionValue + separator;
- string lId = "LicenseId=" + lincese.LicenseId;
- text += lId + separator;
- string customerName = "CustomerName=" + lincese.CustomerName;
- text += customerName + separator;
- string MachineId = "MachineId=" + lincese.MachineId;
- text += MachineId + separator;
- return text;
- }
- /// <summary>
- /// Get License from text file
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- internal static License FromFile(string text)
- {
- License license = new License();
- var stringlist = text.Split(separator);
- var dict = new Dictionary<string, string>();
- foreach (var s in stringlist)
- {
- var arry = s.Split('=');
- if (arry.Length > 1)
- {
- dict.Add(arry[0], arry[1]);
- }
- if (arry.Length == 1)
- {
- dict.Add(arry[0], "");
- }
- }
- var properties = license.GetType().GetProperties();
- foreach (var propertyInfo in properties)
- {
- dict.TryGetValue(propertyInfo.Name, out var value);
- if (string.IsNullOrEmpty(value)) continue;
- object obj;
- switch (propertyInfo.Name)
- {
- case "MaxUsCount":
- obj = Convert.ToInt32(value);
- break;
- case "ExpirationDay":
- obj = Convert.ToInt32(value);
- break;
- case "ExpirationUtcDate":
- obj = Convert.ToDateTime(value);
- break;
- case "MaxAccountCount":
- obj = Convert.ToInt32(value);
- break;
- case "MaxDiskSpace":
- obj = Convert.ToInt32(value);
- break;
- case "LicenseType":
- obj = Enum.Parse(typeof(LicenseType), value);
- break;
- case "Identification":
- obj = value.ToString();
- break;
- case "FunctionalItemValue":
- obj = value.ToString();
- break;
- case "AssociationKey":
- obj = value.ToString();
- break;
- case "UseVersionItemValue":
- obj = value.ToString();
- break;
- case "LicenseId":
- obj = value.ToString();
- break;
- case "CustomerName":
- obj = value.ToString();
- break;
- case "MachineId":
- obj = value.ToString();
- break;
- default:
- obj = value;
- break;
- }
- propertyInfo.SetValue(license, obj);
- }
- return license;
- }
- /// <summary>
- /// Get text from StartupLicnese
- /// </summary>
- /// <param name="lincese">StartupLicnese object</param>
- /// <returns></returns>
- internal static string StartupLicneseToFile(StartupLicnese lincese)
- {
- return "FirstStartupUtcDate=" + lincese.FirstStartupUtcDate;
- }
- /// <summary>
- /// Get StartupLicnese from text File
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- internal static StartupLicnese StartupLicneseFromFile(string text)
- {
- var license = new StartupLicnese();
- var stringlist = text.Split(separator);
- var dict = new Dictionary<string, string>();
- foreach (var s in stringlist)
- {
- var arry = s.Split('=');
- if (arry.Length > 1)
- {
- dict.Add(arry[0], arry[1]);
- }
- if (arry.Length == 1)
- {
- dict.Add(arry[0], "");
- }
- }
- var properties = license.GetType().GetProperties();
- foreach (var propertyInfo in properties)
- {
- dict.TryGetValue(propertyInfo.Name, out var value);
-
- if (string.IsNullOrEmpty(value)) continue;
- object obj;
- switch (propertyInfo.Name)
- {
- case "FirstStartupUtcDate":
- obj = Convert.ToDateTime(value);
- break;
- default:
- obj = value;
- break;
- }
- propertyInfo.SetValue(license, obj);
- }
- return license;
- }
- }
- }
|