LicenseView.xaml.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Aitex.Core.RT.Key;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace MECF.Framework.RT.Core.Backend
  20. {
  21. /// <summary>
  22. /// LicenseView.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class LicenseView : UserControl
  25. {
  26. PeriodicJob _thread;
  27. public LicenseView()
  28. {
  29. InitializeComponent();
  30. _thread = new PeriodicJob(500, OnTimer, "LicenseView Update", false, true);
  31. IsVisibleChanged += LicenseView_IsVisibleChanged;
  32. }
  33. private void LicenseView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  34. {
  35. if (IsVisible)
  36. _thread.Start();
  37. else
  38. _thread.Pause();
  39. }
  40. bool OnTimer()
  41. {
  42. try
  43. {
  44. Application.Current.Dispatcher.Invoke(new Action(() =>
  45. {
  46. MachineCode.Text = KeyManager.Instance.LocalMachineCode;
  47. ExpireDateTime.Text = KeyManager.Instance.ExpireDateTime.ToString();
  48. LeftDays.Text = KeyManager.Instance.LeftDays.ToString();
  49. LastRegisterDateTime.Text = KeyManager.Instance.LastRegisterDateTime.ToString();
  50. LastRegisterDays.Text = KeyManager.Instance.LastRegisterDays.ToString();
  51. CurrentDateTime.Text = KeyManager.Instance.CurrentDateTime.ToString();
  52. }));
  53. }
  54. catch (Exception ex)
  55. {
  56. LOG.Write(ex);
  57. }
  58. return true;
  59. }
  60. private void Copy_Click(object sender, RoutedEventArgs e)
  61. {
  62. string key = MachineCode.Text.Trim();
  63. if (!string.IsNullOrEmpty(key))
  64. {
  65. Clipboard.SetDataObject(key);
  66. System.Windows.MessageBox.Show("机器码已经复制到剪贴板", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  67. }
  68. }
  69. private void Export_Click(object sender, RoutedEventArgs e)
  70. {
  71. string key = MachineCode.Text.Trim();
  72. if (string.IsNullOrEmpty(key))
  73. return;
  74. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  75. sfd.DefaultExt = ".code";
  76. sfd.Filter = ".code|*.*";
  77. sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  78. sfd.FileName = string.Format("jet_machine_code_{0}.code", DateTime.Now.ToString("yyyyMMdd_HHmmssfff"));
  79. if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
  80. return;
  81. try
  82. {
  83. byte[] myByte = System.Text.Encoding.UTF8.GetBytes(key);
  84. using (FileStream fsWrite = new FileStream(sfd.FileName, FileMode.OpenOrCreate))
  85. {
  86. fsWrite.Write(myByte, 0, myByte.Length);
  87. }
  88. System.Windows.MessageBox.Show(string.Format("机器码导出到文件\r\n{0}\r\n成功", sfd.FileName), "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  89. }
  90. catch (Exception ex)
  91. {
  92. System.Windows.MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  93. }
  94. }
  95. private void Paste_Click(object sender, RoutedEventArgs e)
  96. {
  97. if (!Clipboard.ContainsText())
  98. return;
  99. Key.Text = Clipboard.GetText();
  100. }
  101. private void Import_Click(object sender, RoutedEventArgs e)
  102. {
  103. System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
  104. ofd.DefaultExt = ".code";
  105. ofd.Filter = ".code|*.*";
  106. ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  107. //ofd.FileName = string.Format("machine_code_{0}.code", DateTime.Now.ToString("yyyyMMdd_HHmmssfff"));
  108. if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
  109. return;
  110. try
  111. {
  112. using (FileStream fsSource = new FileStream(ofd.FileName,
  113. FileMode.Open, FileAccess.Read))
  114. {
  115. // Read the source file into a byte array.
  116. byte[] bytes = new byte[fsSource.Length];
  117. fsSource.Read(bytes, 0, (int)fsSource.Length);
  118. Key.Text = System.Text.Encoding.UTF8.GetString(bytes);
  119. }
  120. System.Windows.MessageBox.Show("导入注册码成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  121. }
  122. catch (Exception ex)
  123. {
  124. System.Windows.MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  125. }
  126. }
  127. private void Register_Click(object sender, RoutedEventArgs e)
  128. {
  129. string inputKey = Key.Text.Trim();
  130. if (string.IsNullOrEmpty(inputKey))
  131. return;
  132. string reason;
  133. if (!KeyManager.Instance.Register(inputKey, out reason))
  134. {
  135. System.Windows.MessageBox.Show(reason, "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  136. return;
  137. }
  138. System.Windows.MessageBox.Show(reason, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  139. }
  140. }
  141. }