Form1.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Xml;
  12. namespace OTSRegister
  13. {
  14. public partial class Form1 : Form
  15. {
  16. List<string> list_str = new List<string>();
  17. IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. OpenFileDialog ofd = new OpenFileDialog();
  25. if (ofd.ShowDialog() == DialogResult.OK)
  26. {
  27. var a = ReadXML(ofd.FileName);
  28. //List<string> list_str = new List<string>();
  29. List<string> list_time = new List<string>();
  30. a.TryGetValue("ID", out list_str);
  31. label2.Text ="cpu:"+ list_str[0];
  32. label3.Text = "硬盘:"+list_str[1];
  33. a.TryGetValue("Time", out list_time);
  34. string time_Day;
  35. if (Convert.ToInt32(list_time[2]) < 10)
  36. {
  37. time_Day = "0" + list_time[2];
  38. }
  39. else
  40. {
  41. time_Day = list_time[2];
  42. }
  43. string time_Month;
  44. if (Convert.ToInt32(list_time[1]) < 10)
  45. {
  46. time_Month = "0" + list_time[1];
  47. }
  48. else
  49. {
  50. time_Month = list_time[1];
  51. }
  52. label4.Text ="有效期至:"+ list_time[0].ToString() + "-" + time_Month + "-" + time_Day;
  53. DateTime dt = DateTime.ParseExact(list_time[0].ToString() + time_Month + time_Day, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  54. //TimePicker.Value = dt;
  55. }
  56. }
  57. public static IDictionary<String, List<String>> ReadXML(string address)
  58. {
  59. IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();
  60. if (File.Exists(address))
  61. {
  62. XmlDocument xmlDoc = new XmlDocument();
  63. xmlDoc.Load(address);
  64. XmlNode xn = xmlDoc.SelectSingleNode("infos");
  65. XmlNodeList xnl = xn.ChildNodes;
  66. foreach (XmlNode xnf in xnl)
  67. {
  68. XmlElement xe = (XmlElement)xnf;
  69. XmlNode nameNode = xe.SelectSingleNode("ID");
  70. string name = nameNode.InnerText;
  71. Console.WriteLine(name);
  72. XmlNode filelist = xe.SelectSingleNode("filelist");
  73. List<String> list = new List<string>();
  74. foreach (XmlNode item in filelist.ChildNodes)
  75. {
  76. list.Add(item.InnerText);
  77. }
  78. infos.Add(name, list);
  79. }
  80. }
  81. return infos;
  82. }
  83. private string ConvertBinary(string str)
  84. {
  85. string strBytes = null;
  86. byte[] data = Encoding.Unicode.GetBytes(str);
  87. for (int i = 0; i < data.Count(); i++)
  88. {
  89. if (strBytes == null)
  90. {
  91. strBytes = data[i].ToString().PadLeft(2, '0');
  92. }
  93. else
  94. {
  95. strBytes = strBytes + "," + data[i].ToString().PadLeft(2, '0');
  96. }
  97. }
  98. return strBytes;
  99. }
  100. public static void SaveXML(IDictionary<String, List<String>> infos, string address)
  101. {
  102. if (infos == null || infos.Count == 0)
  103. {
  104. return;
  105. }
  106. XmlDocument xmlDoc = new XmlDocument();
  107. XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
  108. xmlDoc.AppendChild(dec);
  109. XmlElement _infos = xmlDoc.CreateElement("infos");
  110. foreach (KeyValuePair<String, List<String>> item in infos)
  111. {
  112. XmlElement info = xmlDoc.CreateElement("info");
  113. XmlElement name = xmlDoc.CreateElement("ID");
  114. name.InnerText = item.Key;
  115. info.AppendChild(name);
  116. XmlNode filelist = xmlDoc.CreateElement("filelist");
  117. info.AppendChild(filelist);
  118. foreach (String number in item.Value)
  119. {
  120. XmlElement filed = xmlDoc.CreateElement("filed");
  121. filed.InnerText = number;
  122. filelist.AppendChild(filed);
  123. }
  124. _infos.AppendChild(info);
  125. }
  126. xmlDoc.AppendChild(_infos);
  127. xmlDoc.Save(address);
  128. }
  129. private void button2_Click(object sender, EventArgs e)
  130. {
  131. SaveFileDialog sfd = new SaveFileDialog();
  132. sfd.Filter = "(*.txt)|*.txt|(*.*)|*.*";
  133. sfd.FileName = "RegistrationProofreading";
  134. if (sfd.ShowDialog() == DialogResult.OK)
  135. {
  136. string cpu = ConvertBinary(list_str[0]);
  137. string Disk = ConvertBinary(list_str[1]);
  138. string Year = ConvertBinary(TimePicker.Value.Year.ToString());
  139. string set_Month;
  140. if (Convert.ToInt32(TimePicker.Value.Month.ToString()) < 10)
  141. {
  142. set_Month = "0" + TimePicker.Value.Month.ToString();
  143. }
  144. else
  145. {
  146. set_Month = TimePicker.Value.Month.ToString();
  147. }
  148. string Month = ConvertBinary(set_Month);
  149. string set_Day;
  150. if (Convert.ToInt32(TimePicker.Value.Day.ToString()) < 10)
  151. {
  152. set_Day = "0" + TimePicker.Value.Day.ToString();
  153. }
  154. else
  155. {
  156. set_Day = TimePicker.Value.Day.ToString();
  157. }
  158. string Day = ConvertBinary(set_Day);
  159. infos.Add("ID", new List<string>() { cpu, Disk });
  160. infos.Add("Time", new List<string>() { Year, Month, Day.ToString() });
  161. SaveXML(infos, sfd.FileName);
  162. }
  163. MessageBox.Show("生成成功!");
  164. }
  165. }
  166. }