LicenseFileHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Vinno.vCloud.Common.License.Licenses
  4. {
  5. public static class LicenseFileHelper
  6. {
  7. /// <summary>
  8. /// used'$'when saving File
  9. /// </summary>
  10. private static char separator = '$';
  11. /// <summary>
  12. /// Get text from Lincese
  13. /// </summary>
  14. /// <param name="lincese"></param>
  15. /// <returns></returns>
  16. public static string LinceseToFile(License lincese)
  17. {
  18. string text = string.Empty;
  19. string expirationUtcDatetext = "ExpirationUtcDate=" + lincese.ExpirationUtcDate;
  20. text += expirationUtcDatetext + separator;
  21. string maxAccountCounttext = "MaxAccountCount=" + lincese.MaxAccountCount;
  22. text += maxAccountCounttext + separator;
  23. string maxUsCounttext = "MaxUsCount=" + lincese.MaxUsCount;
  24. text += maxUsCounttext + separator;
  25. string maxDiskSpacetext = "MaxDiskSpace=" + lincese.MaxDiskSpace;
  26. text += maxDiskSpacetext + separator;
  27. string idtext = "Identification=" + lincese.Identification;
  28. text += idtext + separator;
  29. string funItemValue = "FunctionalItemValue=" + lincese.FunctionalItemValue;
  30. text += funItemValue + separator;
  31. string asskey = "AssociationKey=" + lincese.AssociationKey;
  32. text += asskey + separator;
  33. string useVersionValue = "LicenseType=" + lincese.LicenseType.ToString();
  34. text += useVersionValue + separator;
  35. string lId = "LicenseId=" + lincese.LicenseId;
  36. text += lId + separator;
  37. string customerName = "CustomerName=" + lincese.CustomerName;
  38. text += customerName + separator;
  39. string MachineId = "MachineId=" + lincese.MachineId;
  40. text += MachineId + separator;
  41. return text;
  42. }
  43. /// <summary>
  44. /// Get License from text file
  45. /// </summary>
  46. /// <param name="text"></param>
  47. /// <returns></returns>
  48. internal static License FromFile(string text)
  49. {
  50. License license = new License();
  51. var stringlist = text.Split(separator);
  52. var dict = new Dictionary<string, string>();
  53. foreach (var s in stringlist)
  54. {
  55. var arry = s.Split('=');
  56. if (arry.Length > 1)
  57. {
  58. dict.Add(arry[0], arry[1]);
  59. }
  60. if (arry.Length == 1)
  61. {
  62. dict.Add(arry[0], "");
  63. }
  64. }
  65. var properties = license.GetType().GetProperties();
  66. foreach (var propertyInfo in properties)
  67. {
  68. dict.TryGetValue(propertyInfo.Name, out var value);
  69. if (string.IsNullOrEmpty(value)) continue;
  70. object obj;
  71. switch (propertyInfo.Name)
  72. {
  73. case "MaxUsCount":
  74. obj = Convert.ToInt32(value);
  75. break;
  76. case "ExpirationDay":
  77. obj = Convert.ToInt32(value);
  78. break;
  79. case "ExpirationUtcDate":
  80. obj = Convert.ToDateTime(value);
  81. break;
  82. case "MaxAccountCount":
  83. obj = Convert.ToInt32(value);
  84. break;
  85. case "MaxDiskSpace":
  86. obj = Convert.ToInt32(value);
  87. break;
  88. case "LicenseType":
  89. obj = Enum.Parse(typeof(LicenseType), value);
  90. break;
  91. case "Identification":
  92. obj = value.ToString();
  93. break;
  94. case "FunctionalItemValue":
  95. obj = value.ToString();
  96. break;
  97. case "AssociationKey":
  98. obj = value.ToString();
  99. break;
  100. case "UseVersionItemValue":
  101. obj = value.ToString();
  102. break;
  103. case "LicenseId":
  104. obj = value.ToString();
  105. break;
  106. case "CustomerName":
  107. obj = value.ToString();
  108. break;
  109. case "MachineId":
  110. obj = value.ToString();
  111. break;
  112. default:
  113. obj = value;
  114. break;
  115. }
  116. propertyInfo.SetValue(license, obj);
  117. }
  118. return license;
  119. }
  120. /// <summary>
  121. /// Get text from StartupLicnese
  122. /// </summary>
  123. /// <param name="lincese">StartupLicnese object</param>
  124. /// <returns></returns>
  125. internal static string StartupLicneseToFile(StartupLicnese lincese)
  126. {
  127. return "FirstStartupUtcDate=" + lincese.FirstStartupUtcDate;
  128. }
  129. /// <summary>
  130. /// Get StartupLicnese from text File
  131. /// </summary>
  132. /// <param name="text"></param>
  133. /// <returns></returns>
  134. internal static StartupLicnese StartupLicneseFromFile(string text)
  135. {
  136. var license = new StartupLicnese();
  137. var stringlist = text.Split(separator);
  138. var dict = new Dictionary<string, string>();
  139. foreach (var s in stringlist)
  140. {
  141. var arry = s.Split('=');
  142. if (arry.Length > 1)
  143. {
  144. dict.Add(arry[0], arry[1]);
  145. }
  146. if (arry.Length == 1)
  147. {
  148. dict.Add(arry[0], "");
  149. }
  150. }
  151. var properties = license.GetType().GetProperties();
  152. foreach (var propertyInfo in properties)
  153. {
  154. dict.TryGetValue(propertyInfo.Name, out var value);
  155. if (string.IsNullOrEmpty(value)) continue;
  156. object obj;
  157. switch (propertyInfo.Name)
  158. {
  159. case "FirstStartupUtcDate":
  160. obj = Convert.ToDateTime(value);
  161. break;
  162. default:
  163. obj = value;
  164. break;
  165. }
  166. propertyInfo.SetValue(license, obj);
  167. }
  168. return license;
  169. }
  170. }
  171. }