LicenseView.cs 5.6 KB

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