using System; using System.IO; using System.Windows; using System.Windows.Forms; using Aitex.Core.RT.Key; using Aitex.Core.RT.Log; using Aitex.Core.Util; using Clipboard = System.Windows.Forms.Clipboard; namespace Aitex.Core.Backend { public partial class LicenseView : UserControl { PeriodicJob _thread; public LicenseView() { InitializeComponent(); this.VisibleChanged += LicenseView_VisibleChanged; _thread = new PeriodicJob(500, OnTimer, "LicenseView Update", false, true); } void LicenseView_VisibleChanged(object sender, EventArgs e) { if (Visible) _thread.Start(); else _thread.Pause(); } bool OnTimer() { try { Invoke(new Action(() => { textMachineCode.Text = KeyManager.Instance.LocalMachineCode; textLastDate.Text = KeyManager.Instance.ExpireDateTime.ToString(); textLeftDays.Text = KeyManager.Instance.LeftDays.ToString(); textRegisterDate.Text = KeyManager.Instance.LastRegisterDateTime.ToString(); textRegisterDays.Text = KeyManager.Instance.LastRegisterDays.ToString(); textPlcDateTime.Text = KeyManager.Instance.CurrentDateTime.ToString(); })); } catch (Exception ex) { LOG.Write(ex); } return true; } private void LicenseView_Load(object sender, EventArgs e) { } private void btnRegister_Click(object sender, EventArgs e) { string inputKey = textKey.Text.Trim(); if (string.IsNullOrEmpty(inputKey)) return; string reason; if (!KeyManager.Instance.Register(inputKey, out reason)) { System.Windows.MessageBox.Show(reason, "提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } System.Windows.MessageBox.Show(reason, "提示", MessageBoxButton.OK, MessageBoxImage.Information); } private void btnCopy_Click(object sender, EventArgs e) { string key = textMachineCode.Text.Trim(); if (!string.IsNullOrEmpty(key)) { Clipboard.SetDataObject(key); System.Windows.MessageBox.Show("机器码已经复制到剪贴板", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } } private void btnExport_Click(object sender, EventArgs e) { string key = textMachineCode.Text.Trim(); if (string.IsNullOrEmpty(key)) return; System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog(); sfd.DefaultExt = ".code"; sfd.Filter = ".code|*.*"; sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); sfd.FileName = string.Format("jet_machine_code_{0}.code", DateTime.Now.ToString("yyyyMMdd_HHmmssfff")); if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; try { byte[] myByte = System.Text.Encoding.UTF8.GetBytes(key); using (FileStream fsWrite = new FileStream(sfd.FileName, FileMode.OpenOrCreate)) { fsWrite.Write(myByte, 0, myByte.Length); } System.Windows.MessageBox.Show(string.Format("机器码导出到文件\r\n{0}\r\n成功",sfd.FileName), "提示", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } } private void btnPaste_Click(object sender, EventArgs e) { if (!Clipboard.ContainsText()) return; textKey.Text = Clipboard.GetText(); } private void btnImport_Click(object sender, EventArgs e) { System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog(); ofd.DefaultExt = ".code"; ofd.Filter = ".code|*.*"; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); //ofd.FileName = string.Format("machine_code_{0}.code", DateTime.Now.ToString("yyyyMMdd_HHmmssfff")); if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; try { using (FileStream fsSource = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read)) { // Read the source file into a byte array. byte[] bytes = new byte[fsSource.Length]; fsSource.Read(bytes, 0, (int) fsSource.Length); textKey.Text = System.Text.Encoding.UTF8.GetString(bytes); } System.Windows.MessageBox.Show("导入注册码成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } } } }