LicenseLoader.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace HXX.Scanner.Biz
  7. {
  8. /*
  9. * 第三方弹框获取licence,内部方法
  10. */
  11. class LicenseLoader
  12. {
  13. [DllImport("kernel32.dll")]
  14. static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);
  15. [DllImport("kernel32.dll")]
  16. static extern uint GetWindowsDirectory([Out] StringBuilder lpBuffer, uint uSize);
  17. private static string GetLicenseFilePath()
  18. {
  19. StringBuilder sbsSystemDir = new StringBuilder(256);
  20. uint iLength = 0;
  21. iLength = GetSystemDirectory(sbsSystemDir, 256);
  22. if (iLength == 0)
  23. return null;
  24. StringBuilder sbsWindowsDir = new StringBuilder(256);
  25. iLength = GetWindowsDirectory(sbsWindowsDir, 256);
  26. if (iLength == 0)
  27. return null;
  28. if (sbsWindowsDir.Length > 0)
  29. {
  30. if (IntPtr.Size == 8)
  31. {
  32. sbsWindowsDir.Append("\\SysWOW64\\DynamsoftDotNetTwain.lic");
  33. }
  34. else
  35. {
  36. sbsWindowsDir.Append("\\System32\\DynamsoftDotNetTwain.lic");
  37. }
  38. }
  39. return sbsWindowsDir.ToString();
  40. }
  41. private static List<string> ReadSystemLocalLicense(string strlicensePath)
  42. {
  43. List<string> liststringTempListLicense = null;
  44. try
  45. {
  46. if (File.Exists(strlicensePath))
  47. {
  48. string[] stringarrTempAllLines = File.ReadAllLines(strlicensePath);
  49. foreach (string strTemp in stringarrTempAllLines)
  50. {
  51. int iTempIndex = strTemp.IndexOf("SerialNo");
  52. if (iTempIndex != -1)
  53. {
  54. int iIndex1 = strTemp.IndexOf("=");
  55. if (iIndex1 != -1)
  56. {
  57. if (liststringTempListLicense == null)
  58. liststringTempListLicense = new List<string>();
  59. string strTempLicense = strTemp.Substring((iIndex1 + 1), (strTemp.Length - (iIndex1 + 1)));
  60. liststringTempListLicense.Add(strTempLicense);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. catch
  67. { }
  68. return liststringTempListLicense;
  69. }
  70. private static string GetCurrentPath()
  71. {
  72. string strCurrentPath = System.IO.Directory.GetCurrentDirectory();
  73. string strCurrentPathtxt = strCurrentPath + "\\lic.txt";
  74. return strCurrentPathtxt;
  75. }
  76. private static string ReadLicense(string strPath)
  77. {
  78. try
  79. {
  80. string strLicenseMessage = System.IO.File.ReadAllText(strPath);
  81. int beginIndex = strLicenseMessage.IndexOf("=\"");
  82. int endIndex = strLicenseMessage.IndexOf("\";");
  83. string strLicense = null;
  84. if (beginIndex != -1 && endIndex != -1)
  85. {
  86. strLicense = strLicenseMessage.Substring(beginIndex + 2, endIndex - beginIndex - 2);
  87. }
  88. return strLicense;
  89. }
  90. catch (Exception)
  91. {
  92. string strLicense = null;
  93. return strLicense;
  94. }
  95. }
  96. private static string CombineAllLicense(string strCurrentPathLicense)
  97. {
  98. if (strCurrentPathLicense != null)
  99. {
  100. try
  101. {
  102. string strTempLicenseFilePath = GetLicenseFilePath();
  103. List<string> tempListLicense = ReadSystemLocalLicense(strTempLicenseFilePath);
  104. StringBuilder objProductKeys = new StringBuilder();
  105. foreach (string temp in tempListLicense)
  106. {
  107. if (temp != null)
  108. {
  109. objProductKeys.Append(temp);
  110. objProductKeys.Append(";");
  111. }
  112. }
  113. string strTempProductKey = objProductKeys.ToString();
  114. strTempProductKey = strTempProductKey + strCurrentPathLicense + ";";
  115. return strTempProductKey;
  116. }
  117. catch (Exception)
  118. {
  119. string strTempProductKey = strCurrentPathLicense + ";";
  120. return strTempProductKey;
  121. }
  122. }
  123. else
  124. {
  125. try
  126. {
  127. string steTempLicenseFilePath = GetLicenseFilePath();
  128. List<string> liststringTempListLicense = ReadSystemLocalLicense(steTempLicenseFilePath);
  129. StringBuilder objProductKeys = new StringBuilder();
  130. foreach (string temp in liststringTempListLicense)
  131. {
  132. if (temp != null)
  133. {
  134. objProductKeys.Append(temp);
  135. objProductKeys.Append(";");
  136. }
  137. }
  138. string strTempProductKey = objProductKeys.ToString();
  139. return strTempProductKey;
  140. }
  141. catch (Exception)
  142. {
  143. return null;
  144. }
  145. }
  146. }
  147. public static string ReadLocalLicense()
  148. {
  149. string strGetCurrentPath = LicenseLoader.GetCurrentPath();
  150. string strReadLicense = LicenseLoader.ReadLicense(strGetCurrentPath);
  151. string strCombineAllLicense = LicenseLoader.CombineAllLicense(strReadLicense);
  152. return strCombineAllLicense;
  153. }
  154. }
  155. }