MainWindow.xaml.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. using System.IO;
  2. using System.Reflection;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Documents;
  7. using System.Windows.Input;
  8. using System.Windows.Media.Imaging;
  9. using Microsoft.Win32;
  10. using Org.BouncyCastle.Crypto.Engines;
  11. using Org.BouncyCastle.Crypto.Modes;
  12. using Org.BouncyCastle.Crypto.Paddings;
  13. using Org.BouncyCastle.Crypto.Parameters;
  14. namespace ArchivesCenter3
  15. {
  16. /// <summary>
  17. /// Interaction logic for MainWindow.xaml
  18. /// </summary>
  19. public partial class MainWindow : Window
  20. {
  21. public MainWindow()
  22. {
  23. InitializeComponent();
  24. updateinfo();
  25. LoadFileContent();
  26. levelitemupdate();
  27. tagitemupdate();
  28. Birthplaceitemupdate();
  29. RelativeStatusitemupdate();
  30. Load4Config();
  31. MyCalendar.DisplayDate = DateTime.Today;
  32. MyCalendar.SelectedDate = DateTime.Today;
  33. Version version = Assembly.GetExecutingAssembly().GetName().Version;
  34. versionTextBlock.Text = $"版本号:{version.Major}.{version.Minor}.{version.Build}";
  35. DateTime tomorrow = DateTime.Today.AddDays(1);
  36. // 创建一个从明天开始到最大日期的不可选日期范围
  37. CalendarDateRange blackoutRange = new CalendarDateRange(tomorrow, DateTime.MaxValue);
  38. // 将该范围添加到 Calendar 的 BlackoutDates 集合中
  39. MyCalendar.BlackoutDates.Add(blackoutRange);
  40. }
  41. private List<string> lines; // 存储文件内容的列表
  42. private int currentIndex = -1; // 当前行索引
  43. private bool isNewRecord = false; // 标记是否是新建记录
  44. private void updateinfo()
  45. {
  46. welcomeText.Text = "欢迎!" + Settings1.Default.Username;
  47. WelcomeTitle.Text = Settings1.Default.WelcomeTitle;
  48. DatabaseName.Text = Settings1.Default.DatabaseName;
  49. DatabaseSubtitle.Text = Settings1.Default.DatabaseSubtitle;
  50. UsernameCu.Text = Settings1.Default.Username;
  51. WelcomeTitleCu.Text = Settings1.Default.WelcomeTitle;
  52. DatabaseNameCu.Text = Settings1.Default.DatabaseName;
  53. DatabaseSubtitleCu.Text = Settings1.Default.DatabaseSubtitle;
  54. PasswordCu.Text = Settings1.Default.Password;
  55. DatabaseNameBox.Text = null;
  56. DatabaseSubtitleBox.Text = null;
  57. WelcomeTitleBox.Text = null;
  58. UsernameBox.Text = null;
  59. PasswordBox.Text = null;
  60. }
  61. private void Button_Click(object sender, RoutedEventArgs e)
  62. {
  63. if (!string.IsNullOrEmpty(DatabaseNameBox.Text))
  64. Settings1.Default.DatabaseName = DatabaseNameBox.Text;
  65. if (!string.IsNullOrEmpty(DatabaseSubtitleBox.Text))
  66. Settings1.Default.DatabaseSubtitle = DatabaseSubtitleBox.Text;
  67. if (!string.IsNullOrEmpty(WelcomeTitleBox.Text))
  68. Settings1.Default.WelcomeTitle = WelcomeTitleBox.Text;
  69. if (!string.IsNullOrEmpty(UsernameBox.Text))
  70. Settings1.Default.Username = UsernameBox.Text;
  71. if (!string.IsNullOrEmpty(PasswordBox.Text))
  72. Settings1.Default.Password = PasswordBox.Text;
  73. Settings1.Default.Save();
  74. updateinfo();
  75. string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  76. string levelConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "level.config");
  77. string tagConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "tag.config");
  78. string relativeStatusConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "relativeStatus.config");
  79. string birthplaceConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "birthplace.config");
  80. // 保存配置文件内容
  81. File.WriteAllText(levelConfigPath, LevelConfigBox.Text);
  82. File.WriteAllText(tagConfigPath, TagConfigBox.Text);
  83. File.WriteAllText(relativeStatusConfigPath, RSConfigBox.Text);
  84. File.WriteAllText(birthplaceConfigPath, BPConfigBox.Text);
  85. }
  86. private void TextChanged(object sender, TextChangedEventArgs e)
  87. {
  88. bool allTextBoxesEmpty = string.IsNullOrWhiteSpace(DatabaseNameBox.Text) &&
  89. string.IsNullOrWhiteSpace(DatabaseSubtitleBox.Text) &&
  90. string.IsNullOrWhiteSpace(WelcomeTitleBox.Text) &&
  91. string.IsNullOrWhiteSpace(UsernameBox.Text) &&
  92. string.IsNullOrWhiteSpace(LevelConfigBox.Text) &&
  93. string.IsNullOrWhiteSpace(TagConfigBox.Text) &&
  94. string.IsNullOrWhiteSpace(RSConfigBox.Text) &&
  95. string.IsNullOrWhiteSpace(BPConfigBox.Text) &&
  96. string.IsNullOrWhiteSpace(PasswordBox.Text);
  97. settingSaveButton0.IsEnabled = !allTextBoxesEmpty;
  98. settingSaveButton1.IsEnabled = !allTextBoxesEmpty;
  99. }
  100. private void LoadFileContent()
  101. {
  102. // 获取用户文档文件夹路径
  103. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  104. // 构建目标文件路径
  105. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.info");
  106. // 检查文件是否存在
  107. if (File.Exists(filePath))
  108. {
  109. // 读取文件内容
  110. lines = File.ReadAllLines(filePath).ToList();
  111. var people = new List<Person>();
  112. foreach (var line in lines)
  113. {
  114. string[] parts = line.Split(new string[] { "**" }, StringSplitOptions.None);
  115. if (parts.Length > 26) // 确保有数据且排除图片路径
  116. {
  117. people.Add(new Person
  118. {
  119. Name = parts.Length > 0 ? parts[0] : string.Empty,
  120. Gender = parts.Length > 1 ? parts[1] : string.Empty,
  121. BirthDate = parts.Length > 2 ? parts[2] : string.Empty,
  122. Level = parts.Length > 3 ? parts[3] : string.Empty,
  123. Tag = parts.Length > 4 ? parts[4] : string.Empty,
  124. IDType = parts.Length > 5 ? parts[5] : string.Empty,
  125. IDNumber = parts.Length > 6 ? parts[6] : string.Empty,
  126. Birthplace = parts.Length > 7 ? parts[7] : string.Empty,
  127. PoliticalStatus = parts.Length > 8 ? parts[8] : string.Empty,
  128. MaritalStatus = parts.Length > 9 ? parts[9] : string.Empty,
  129. Address = parts.Length > 10 ? parts[10] : string.Empty,
  130. FamilySituation = parts.Length > 11 ? parts[11] : string.Empty,
  131. AcquaintancePlace = parts.Length > 12 ? parts[12] : string.Empty,
  132. RelativeStatus = parts.Length > 13 ? parts[13] : string.Empty,
  133. Education = parts.Length > 14 ? parts[14] : string.Empty,
  134. PrimarySchool = parts.Length > 15 ? parts[15] : string.Empty,
  135. JuniorHighSchool = parts.Length > 16 ? parts[16] : string.Empty,
  136. HighSchool = parts.Length > 17 ? parts[17] : string.Empty,
  137. University = parts.Length > 18 ? parts[18] : string.Empty,
  138. GraduateSchool = parts.Length > 19 ? parts[19] : string.Empty,
  139. WorkUnit = parts.Length > 20 ? parts[20] : string.Empty,
  140. PhoneNumber = parts.Length > 21 ? parts[21] : string.Empty,
  141. Email = parts.Length > 22 ? parts[22] : string.Empty,
  142. WeChat = parts.Length > 23 ? parts[23] : string.Empty,
  143. QQ = parts.Length > 24 ? parts[24] : string.Empty,
  144. OtherContact = parts.Length > 25 ? parts[25] : string.Empty
  145. });
  146. }
  147. }
  148. // 绑定数据到 DataGrid
  149. dataGrid.ItemsSource = people;
  150. }
  151. else
  152. {
  153. lines = new List<string>();
  154. dataGrid.ItemsSource = new List<Person>();
  155. }
  156. }
  157. private void DisplayCurrentLine()
  158. {
  159. if (currentIndex >= 0 && currentIndex < lines.Count)
  160. {
  161. string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None);
  162. txtName.Text = parts.Length > 0 ? parts[0] : string.Empty;
  163. cmbGender.SelectedItem = GetComboBoxItem(cmbGender, parts.Length > 1 ? parts[1] : null);
  164. dpBirthDate.SelectedDate = parts.Length > 2 ? DateTime.Parse(parts[2]) : (DateTime?)null;
  165. cmbLevel.SelectedItem = GetComboBoxItem(cmbLevel, parts.Length > 3 ? parts[3] : null);
  166. cmbTag.SelectedItem = GetComboBoxItem(cmbTag, parts.Length > 4 ? parts[4] : null);
  167. cmbIDType.SelectedItem = GetComboBoxItem(cmbIDType, parts.Length > 5 ? parts[5] : null);
  168. txtIDNumber.Text = parts.Length > 6 ? parts[6] : string.Empty;
  169. cmbBirthplace.SelectedItem = GetComboBoxItem(cmbBirthplace, parts.Length > 7 ? parts[7] : null);
  170. cmbPoliticalStatus.SelectedItem = GetComboBoxItem(cmbPoliticalStatus, parts.Length > 8 ? parts[8] : null);
  171. cmbMaritalStatus.SelectedItem = GetComboBoxItem(cmbMaritalStatus, parts.Length > 9 ? parts[9] : null);
  172. txtAddress.Text = parts.Length > 10 ? parts[10] : string.Empty;
  173. txtFamilySituation.Text = parts.Length > 11 ? parts[11] : string.Empty;
  174. txtAcquaintancePlace.Text = parts.Length > 12 ? parts[12] : string.Empty;
  175. cmbRelativeStatus.SelectedItem = GetComboBoxItem(cmbRelativeStatus, parts.Length > 13 ? parts[13] : null);
  176. cmbEducation.SelectedItem = GetComboBoxItem(cmbEducation, parts.Length > 14 ? parts[14] : string.Empty);
  177. txtPrimarySchool.Text = parts.Length > 15 ? parts[15] : string.Empty;
  178. txtJuniorHighSchool.Text = parts.Length > 16 ? parts[16] : string.Empty;
  179. txtHighSchool.Text = parts.Length > 17 ? parts[17] : string.Empty;
  180. txtUniversity.Text = parts.Length > 18 ? parts[18] : string.Empty;
  181. txtGraduateSchool.Text = parts.Length > 19 ? parts[19] : string.Empty;
  182. txtWorkUnit.Text = parts.Length > 20 ? parts[20] : string.Empty;
  183. txtPhoneNumber.Text = parts.Length > 21 ? parts[21] : string.Empty;
  184. txtEmail.Text = parts.Length > 22 ? parts[22] : string.Empty;
  185. txtWeChat.Text = parts.Length > 23 ? parts[23] : string.Empty;
  186. txtQQ.Text = parts.Length > 24 ? parts[24] : string.Empty;
  187. txtOtherContact.Text = parts.Length > 25 ? parts[25] : string.Empty;
  188. // 更新图片显示控件
  189. if (parts.Length > 26 && !string.IsNullOrEmpty(parts[26]))
  190. {
  191. try
  192. {
  193. BitmapImage bitmap = new BitmapImage();
  194. bitmap.BeginInit();
  195. bitmap.UriSource = new Uri(parts[26], UriKind.Absolute);
  196. bitmap.EndInit();
  197. imgPhoto.Source = bitmap;
  198. }
  199. catch
  200. {
  201. // 如果图片路径无效,清空图片控件
  202. imgPhoto.Source = null;
  203. }
  204. }
  205. else
  206. {
  207. // 如果没有图片路径,清空图片控件
  208. imgPhoto.Source = null;
  209. }
  210. }
  211. else
  212. {
  213. ClearControls();
  214. }
  215. }
  216. private void ClearControls()
  217. {
  218. txtName.Clear();
  219. cmbGender.SelectedItem = null;
  220. dpBirthDate.SelectedDate = new DateTime(1900, 1, 1); // 设置默认日期为 1900-01-01
  221. dpBirthDate.Text = "1900/1/1";
  222. cmbLevel.SelectedItem = null;
  223. cmbTag.SelectedItem = null;
  224. cmbIDType.SelectedItem = null;
  225. txtIDNumber.Clear();
  226. cmbBirthplace.SelectedItem = null;
  227. cmbPoliticalStatus.SelectedItem = null;
  228. cmbMaritalStatus.SelectedItem = null;
  229. txtAddress.Clear();
  230. txtFamilySituation.Clear();
  231. txtAcquaintancePlace.Clear();
  232. cmbRelativeStatus.SelectedItem = null;
  233. cmbEducation.SelectedItem = null;
  234. txtPrimarySchool.Clear();
  235. txtJuniorHighSchool.Clear();
  236. txtHighSchool.Clear();
  237. txtUniversity.Clear();
  238. txtGraduateSchool.Clear();
  239. txtWorkUnit.Clear();
  240. txtPhoneNumber.Clear();
  241. txtEmail.Clear();
  242. txtWeChat.Clear();
  243. txtQQ.Clear();
  244. txtOtherContact.Clear();
  245. imgPhoto.Source = null;
  246. }
  247. private object GetComboBoxItem(ComboBox comboBox, string content)
  248. {
  249. if (string.IsNullOrEmpty(content)) return null;
  250. foreach (var item in comboBox.Items)
  251. {
  252. if (item.ToString() == content)
  253. {
  254. return item;
  255. }
  256. }
  257. return null;
  258. }
  259. private void PreviousButton_Click(object sender, RoutedEventArgs e)
  260. {
  261. if (currentIndex > 0)
  262. {
  263. currentIndex--;
  264. DisplayCurrentLine();
  265. }
  266. }
  267. private void NextButton_Click(object sender, RoutedEventArgs e)
  268. {
  269. if (currentIndex < lines.Count - 1)
  270. {
  271. currentIndex++;
  272. DisplayCurrentLine();
  273. }
  274. }
  275. private void SaveButton_Click(object sender, RoutedEventArgs e)
  276. {
  277. // 获取所有控件的值
  278. string name = txtName.Text;
  279. string gender = cmbGender.SelectedItem?.ToString();
  280. string birthDate = dpBirthDate.SelectedDate?.ToString("yyyy-MM-dd") ?? string.Empty;
  281. string level = cmbLevel.SelectedItem?.ToString();
  282. string tag = cmbTag.SelectedItem?.ToString();
  283. string idType = cmbIDType.SelectedItem?.ToString();
  284. string idNumber = txtIDNumber.Text;
  285. string birthplace = cmbBirthplace.SelectedItem?.ToString();
  286. string politicalStatus = cmbPoliticalStatus.SelectedItem?.ToString();
  287. string maritalStatus = cmbMaritalStatus.SelectedItem?.ToString();
  288. string address = txtAddress.Text;
  289. string familySituation = txtFamilySituation.Text;
  290. string acquaintancePlace = txtAcquaintancePlace.Text;
  291. string relativeStatus = cmbRelativeStatus.SelectedItem?.ToString();
  292. string education = cmbEducation.SelectedItem?.ToString();
  293. string primarySchool = txtPrimarySchool.Text;
  294. string juniorHighSchool = txtJuniorHighSchool.Text;
  295. string highSchool = txtHighSchool.Text;
  296. string university = txtUniversity.Text;
  297. string graduateSchool = txtGraduateSchool.Text;
  298. string workUnit = txtWorkUnit.Text;
  299. string phoneNumber = txtPhoneNumber.Text;
  300. string email = txtEmail.Text;
  301. string weChat = txtWeChat.Text;
  302. string qq = txtQQ.Text;
  303. string otherContact = txtOtherContact.Text;
  304. string photoPath = imgPhoto.Source?.ToString() ?? string.Empty;
  305. // 构建保存的内容,使用两个星号 ** 分割每一列
  306. string content = string.Join("**", new string[]
  307. {
  308. name, gender, birthDate, level, tag, idType, idNumber, birthplace,
  309. politicalStatus, maritalStatus, address, familySituation, acquaintancePlace,
  310. relativeStatus, education, primarySchool, juniorHighSchool, highSchool,
  311. university, graduateSchool, workUnit, phoneNumber, email, weChat, qq, otherContact, photoPath
  312. });
  313. // 获取用户文档文件夹路径
  314. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  315. // 构建目标文件路径
  316. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.info");
  317. // 确保目标文件夹存在
  318. string directoryPath = Path.GetDirectoryName(filePath);
  319. if (!Directory.Exists(directoryPath))
  320. {
  321. Directory.CreateDirectory(directoryPath);
  322. }
  323. if (currentIndex >= 0 && currentIndex < lines.Count)
  324. {
  325. // 修改当前行的内容
  326. lines[currentIndex] = content;
  327. }
  328. else
  329. {
  330. // 新增一行
  331. lines.Add(content);
  332. }
  333. // 保存文件内容
  334. File.WriteAllLines(filePath, lines);
  335. // 更新 DataGrid
  336. LoadFileContent();
  337. MessageBox.Show("保存成功!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Information);
  338. }
  339. private void NewButton_Click(object sender, RoutedEventArgs e)
  340. {
  341. // 清空所有控件的内容
  342. ClearControls();
  343. // 如果是第一次点击新建按钮,添加一个空行
  344. if (!isNewRecord)
  345. {
  346. lines.Add(string.Empty);
  347. isNewRecord = true;
  348. }
  349. // 设置当前索引为最后一行
  350. currentIndex = lines.Count - 1;
  351. // 显示新行
  352. DisplayCurrentLine();
  353. }
  354. private void ChangePhotoButton_Click(object sender, RoutedEventArgs e)
  355. {
  356. OpenFileDialog openFileDialog = new OpenFileDialog
  357. {
  358. Filter = "Image Files (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png"
  359. };
  360. if (openFileDialog.ShowDialog() == true)
  361. {
  362. // 获取用户文档文件夹路径
  363. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  364. // 构建目标文件夹路径
  365. string targetDirectory = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people");
  366. // 确保目标文件夹存在
  367. if (!Directory.Exists(targetDirectory))
  368. {
  369. Directory.CreateDirectory(targetDirectory);
  370. }
  371. // 生成随机文件名
  372. string randomFileName = Guid.NewGuid().ToString() + Path.GetExtension(openFileDialog.FileName);
  373. string targetFilePath = Path.Combine(targetDirectory, randomFileName);
  374. // 复制文件到目标路径
  375. File.Copy(openFileDialog.FileName, targetFilePath, true);
  376. // 更新图片显示控件
  377. BitmapImage bitmap = new BitmapImage();
  378. bitmap.BeginInit();
  379. bitmap.UriSource = new Uri(targetFilePath, UriKind.Absolute);
  380. bitmap.EndInit();
  381. imgPhoto.Source = bitmap;
  382. // 如果当前行存在,更新文件路径到 content.info 文件
  383. if (currentIndex >= 0 && currentIndex < lines.Count)
  384. {
  385. string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None);
  386. if (parts.Length > 26)
  387. {
  388. parts[26] = targetFilePath;
  389. }
  390. else
  391. {
  392. parts = parts.Concat(new string[] { targetFilePath }).ToArray();
  393. }
  394. lines[currentIndex] = string.Join("**", parts);
  395. SaveFileContent();
  396. }
  397. }
  398. }
  399. private void SaveFileContent()
  400. {
  401. // 获取用户文档文件夹路径
  402. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  403. // 构建目标文件路径
  404. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.info");
  405. // 确保目标文件夹存在
  406. string directoryPath = Path.GetDirectoryName(filePath);
  407. if (!Directory.Exists(directoryPath))
  408. {
  409. Directory.CreateDirectory(directoryPath);
  410. }
  411. // 保存文件内容
  412. File.WriteAllLines(filePath, lines);
  413. }
  414. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  415. {
  416. if (currentIndex >= 0 && currentIndex < lines.Count)
  417. {
  418. // 弹出确认对话框
  419. MessageBoxResult result = MessageBox.Show("确定要删除当前记录吗?", "Archives Center", MessageBoxButton.YesNo, MessageBoxImage.Question);
  420. if (result == MessageBoxResult.Yes)
  421. {
  422. string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None);
  423. // 检查是否有图片路径,并删除图片文件
  424. if (parts.Length > 26 && !string.IsNullOrEmpty(parts[26]))
  425. {
  426. string photoPath = parts[26];
  427. if (File.Exists(photoPath))
  428. {
  429. File.Delete(photoPath);
  430. }
  431. }
  432. // 如果当前处于编辑状态,删除当前行
  433. lines.RemoveAt(currentIndex);
  434. // 保存文件内容
  435. SaveFileContent();
  436. // 如果删除后还有行,显示上一行的内容
  437. if (currentIndex > 0)
  438. {
  439. currentIndex--;
  440. DisplayCurrentLine();
  441. }
  442. else
  443. {
  444. ClearControls();
  445. }
  446. LoadFileContent();
  447. }
  448. }
  449. else
  450. {
  451. // 如果当前处于新建状态,清空控件内容
  452. ClearControls();
  453. }
  454. }
  455. private void pSearchButton_Click(object sender, RoutedEventArgs e)
  456. {
  457. string searchContent = pSearchBox.Text.Trim(); // 获取搜索框中的内容
  458. if (string.IsNullOrEmpty(searchContent))
  459. {
  460. MessageBox.Show("请输入搜索内容!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Exclamation);
  461. return;
  462. }
  463. // 获取用户文档文件夹路径
  464. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  465. // 构建目标文件路径
  466. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.info");
  467. if (File.Exists(filePath))
  468. {
  469. List<Person> searchResults = new List<Person>();
  470. string[] lines = File.ReadAllLines(filePath);
  471. foreach (var line in lines)
  472. {
  473. string[] parts = line.Split(new string[] { "**" }, StringSplitOptions.None);
  474. if (parts.Length > 26) // 确保有数据且排除图片路径
  475. {
  476. // 检查每一列是否包含搜索内容
  477. if (parts.Any(part => part.Contains(searchContent)))
  478. {
  479. searchResults.Add(new Person
  480. {
  481. Name = parts.Length > 0 ? parts[0] : string.Empty,
  482. Gender = parts.Length > 1 ? parts[1] : string.Empty,
  483. BirthDate = parts.Length > 2 ? parts[2] : string.Empty,
  484. Level = parts.Length > 3 ? parts[3] : string.Empty,
  485. Tag = parts.Length > 4 ? parts[4] : string.Empty,
  486. IDType = parts.Length > 5 ? parts[5] : string.Empty,
  487. IDNumber = parts.Length > 6 ? parts[6] : string.Empty,
  488. Birthplace = parts.Length > 7 ? parts[7] : string.Empty,
  489. PoliticalStatus = parts.Length > 8 ? parts[8] : string.Empty,
  490. MaritalStatus = parts.Length > 9 ? parts[9] : string.Empty,
  491. Address = parts.Length > 10 ? parts[10] : string.Empty,
  492. FamilySituation = parts.Length > 11 ? parts[11] : string.Empty,
  493. AcquaintancePlace = parts.Length > 12 ? parts[12] : string.Empty,
  494. RelativeStatus = parts.Length > 13 ? parts[13] : string.Empty,
  495. Education = parts.Length > 14 ? parts[14] : string.Empty,
  496. PrimarySchool = parts.Length > 15 ? parts[15] : string.Empty,
  497. JuniorHighSchool = parts.Length > 16 ? parts[16] : string.Empty,
  498. HighSchool = parts.Length > 17 ? parts[17] : string.Empty,
  499. University = parts.Length > 18 ? parts[18] : string.Empty,
  500. GraduateSchool = parts.Length > 19 ? parts[19] : string.Empty,
  501. WorkUnit = parts.Length > 20 ? parts[20] : string.Empty,
  502. PhoneNumber = parts.Length > 21 ? parts[21] : string.Empty,
  503. Email = parts.Length > 22 ? parts[22] : string.Empty,
  504. WeChat = parts.Length > 23 ? parts[23] : string.Empty,
  505. QQ = parts.Length > 24 ? parts[24] : string.Empty,
  506. OtherContact = parts.Length > 25 ? parts[25] : string.Empty
  507. });
  508. }
  509. }
  510. }
  511. // 绑定搜索结果到 DataGrid
  512. pSearchdataGrid.ItemsSource = searchResults;
  513. }
  514. else
  515. {
  516. MessageBox.Show("未找到数据文件!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Error);
  517. }
  518. }
  519. private void LoadLevels_Click(object sender, RoutedEventArgs e)
  520. {
  521. levelitemupdate();
  522. }
  523. private void levelitemupdate()
  524. {
  525. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  526. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "config", "level.config");
  527. string directoryPath = Path.GetDirectoryName(filePath);
  528. if (!Directory.Exists(directoryPath))
  529. {
  530. Directory.CreateDirectory(directoryPath);
  531. }
  532. if (!File.Exists(filePath))
  533. File.Create(filePath).Close();
  534. string[] lines = File.ReadAllLines(filePath);
  535. cmbLevel.Items.Clear();
  536. foreach (string line in lines)
  537. {
  538. cmbLevel.Items.Add(line);
  539. }
  540. }
  541. private void LoadTags_Click(object sender, RoutedEventArgs e)
  542. {
  543. tagitemupdate();
  544. }
  545. private void tagitemupdate()
  546. {
  547. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  548. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "config", "tag.config");
  549. string directoryPath = Path.GetDirectoryName(filePath);
  550. if (!Directory.Exists(directoryPath))
  551. {
  552. Directory.CreateDirectory(directoryPath);
  553. }
  554. if (!File.Exists(filePath))
  555. File.Create(filePath).Close();
  556. string[] lines = File.ReadAllLines(filePath);
  557. cmbTag.Items.Clear();
  558. foreach (string line in lines)
  559. {
  560. cmbTag.Items.Add(line);
  561. }
  562. }
  563. private void LoadBirthplaces_Click(object sender, RoutedEventArgs e)
  564. {
  565. Birthplaceitemupdate();
  566. }
  567. private void Birthplaceitemupdate()
  568. {
  569. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  570. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "config", "birthplace.config");
  571. string directoryPath = Path.GetDirectoryName(filePath);
  572. if (!Directory.Exists(directoryPath))
  573. {
  574. Directory.CreateDirectory(directoryPath);
  575. }
  576. if (!File.Exists(filePath))
  577. File.Create(filePath).Close();
  578. string[] lines = File.ReadAllLines(filePath);
  579. cmbBirthplace.Items.Clear();
  580. foreach (string line in lines)
  581. {
  582. cmbBirthplace.Items.Add(line);
  583. }
  584. }
  585. private void LoadRelativeStatuss_Click(object sender, RoutedEventArgs e)
  586. {
  587. RelativeStatusitemupdate();
  588. }
  589. private void RelativeStatusitemupdate()
  590. {
  591. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  592. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "config", "relativeStatus.config");
  593. string directoryPath = Path.GetDirectoryName(filePath);
  594. if (!Directory.Exists(directoryPath))
  595. {
  596. Directory.CreateDirectory(directoryPath);
  597. }
  598. if (!File.Exists(filePath))
  599. File.Create(filePath).Close();
  600. string[] lines = File.ReadAllLines(filePath);
  601. cmbRelativeStatus.Items.Clear();
  602. foreach (string line in lines)
  603. {
  604. cmbRelativeStatus.Items.Add(line);
  605. }
  606. }
  607. private void Load4Config()
  608. {
  609. // 定义配置文件路径
  610. string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  611. string levelConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "level.config");
  612. string tagConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "tag.config");
  613. string relativeStatusConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "relativeStatus.config");
  614. string birthplaceConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "birthplace.config");
  615. // 读取配置文件内容
  616. LevelConfigBox.Text = File.Exists(levelConfigPath) ? File.ReadAllText(levelConfigPath) : string.Empty;
  617. TagConfigBox.Text = File.Exists(tagConfigPath) ? File.ReadAllText(tagConfigPath) : string.Empty;
  618. RSConfigBox.Text = File.Exists(relativeStatusConfigPath) ? File.ReadAllText(relativeStatusConfigPath) : string.Empty;
  619. BPConfigBox.Text = File.Exists(birthplaceConfigPath) ? File.ReadAllText(birthplaceConfigPath) : string.Empty;
  620. }
  621. private void MyCalendar_DisplayDateChanged(object sender, SelectionChangedEventArgs e)
  622. {
  623. // 获取用户文档库路径
  624. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  625. string archivesPath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "log");
  626. // 获取选中的日期
  627. DateTime selectedDate = MyCalendar.SelectedDate.Value;
  628. // 格式化日期为文件夹名称(例如:2025-02-13)
  629. string dateFolderName = selectedDate.ToString("yyyy-MM-dd");
  630. string dateFolderPath = Path.Combine(archivesPath, dateFolderName);
  631. // 检查日期文件夹是否存在
  632. if (Directory.Exists(dateFolderPath))
  633. {
  634. // 如果存在,读取content.txt文件
  635. string contentFilePath = Path.Combine(dateFolderPath, "content.txt");
  636. if (File.Exists(contentFilePath))
  637. {
  638. // 读取文件内容到RichTextBox
  639. string content = File.ReadAllText(contentFilePath);
  640. MyRichTextBox.Document.Blocks.Clear();
  641. MyRichTextBox.Document.Blocks.Add(new Paragraph(new Run(content)));
  642. }
  643. }
  644. else
  645. {
  646. // 如果不存在,创建文件夹和content.txt文件
  647. Directory.CreateDirectory(dateFolderPath);
  648. string contentFilePath = Path.Combine(dateFolderPath, "content.txt");
  649. File.WriteAllText(contentFilePath, ""); // 创建空文件
  650. MyRichTextBox.Document.Blocks.Clear();
  651. }
  652. }
  653. private void LogSaveButton_Click(object sender, RoutedEventArgs e)
  654. {
  655. // 获取用户文档库路径
  656. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  657. string archivesPath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "log");
  658. // 获取选中的日期
  659. DateTime selectedDate = MyCalendar.SelectedDate.Value;
  660. // 格式化日期为文件夹名称(例如:2025-02-13)
  661. string dateFolderName = selectedDate.ToString("yyyy-MM-dd");
  662. string dateFolderPath = Path.Combine(archivesPath, dateFolderName);
  663. // 获取content.txt文件路径
  664. string contentFilePath = Path.Combine(dateFolderPath, "content.txt");
  665. // 获取RichTextBox中的内容
  666. TextRange textRange = new TextRange(MyRichTextBox.Document.ContentStart, MyRichTextBox.Document.ContentEnd);
  667. string content = textRange.Text;
  668. // 将内容写入文件
  669. File.WriteAllText(contentFilePath, content);
  670. MessageBox.Show("保存成功!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Information);
  671. }
  672. private void LogDeleteButton_Click(object sender, RoutedEventArgs e)
  673. {
  674. // 获取用户文档库路径
  675. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  676. string archivesPath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "log");
  677. // 获取选中的日期
  678. DateTime selectedDate = MyCalendar.SelectedDate.Value;
  679. // 格式化日期为文件夹名称(例如:2025-02-13)
  680. string dateFolderName = selectedDate.ToString("yyyy-MM-dd");
  681. string dateFolderPath = Path.Combine(archivesPath, dateFolderName);
  682. // 检查文件夹是否存在
  683. if (Directory.Exists(dateFolderPath))
  684. {
  685. // 删除文件夹及其内容
  686. Directory.Delete(dateFolderPath, true);
  687. MyRichTextBox.Document.Blocks.Clear();
  688. MessageBox.Show("文件夹已删除!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Information);
  689. }
  690. else
  691. {
  692. MessageBox.Show("该日期文件夹不存在!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Error);
  693. }
  694. }
  695. private void SearchButton_Click(object sender, RoutedEventArgs e)
  696. {
  697. // 获取用户文档库路径
  698. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  699. string archivesPath = Path.Combine(userDocumentsPath, "ArchivesCenter", "Data", "Log");
  700. // 获取搜索内容
  701. string searchText = SearchTextBox.Text.Trim();
  702. // 清空 DataGrid
  703. ResultDataGrid.ItemsSource = null;
  704. ResultDataGrid.Items.Clear();
  705. // 如果搜索内容为空,直接返回
  706. if (string.IsNullOrEmpty(searchText))
  707. {
  708. MessageBox.Show("请输入搜索内容!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  709. return;
  710. }
  711. // 遍历所有日期文件夹
  712. try
  713. {
  714. List<string> matchingDates = new List<string>();
  715. foreach (var dateFolder in Directory.GetDirectories(archivesPath))
  716. {
  717. string contentFilePath = Path.Combine(dateFolder, "content.txt");
  718. // 检查文件是否存在
  719. if (File.Exists(contentFilePath))
  720. {
  721. string content = File.ReadAllText(contentFilePath);
  722. // 检查内容是否包含搜索文本
  723. if (content.Contains(searchText, StringComparison.OrdinalIgnoreCase))
  724. {
  725. string dateFolderName = Path.GetFileName(dateFolder);
  726. matchingDates.Add(dateFolderName);
  727. }
  728. }
  729. }
  730. // 将匹配的日期显示到 DataGrid
  731. ResultDataGrid.ItemsSource = matchingDates;
  732. }
  733. catch (Exception ex)
  734. {
  735. MessageBox.Show($"搜索失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  736. }
  737. }
  738. private static byte[] key = Encoding.UTF8.GetBytes("8f4a3b5c6d7e9f1a2b3c4d5e6f7a8b9c");
  739. private void Button_Click_2(object sender, RoutedEventArgs e)
  740. {
  741. string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  742. string targetFolderPath = Path.Combine(documentsPath, "ArchivesCenter");
  743. EncryptFolder(targetFolderPath);
  744. this.Close();
  745. }
  746. public static void EncryptFolder(string folderPath)
  747. {
  748. foreach (string filePath in Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories))
  749. {
  750. byte[] fileContent = File.ReadAllBytes(filePath);
  751. byte[] encryptedContent = Encrypt(fileContent);
  752. File.WriteAllBytes(filePath, encryptedContent);
  753. Console.WriteLine($"Encrypted: {filePath}");
  754. }
  755. }
  756. private static readonly byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
  757. private static byte[] Encrypt(byte[] plaintext)
  758. {
  759. var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new Pkcs7Padding());
  760. var keyParam = new KeyParameter(key);
  761. var parameters = new ParametersWithIV(keyParam, iv);
  762. cipher.Init(true, parameters);
  763. byte[] output = new byte[cipher.GetOutputSize(plaintext.Length)];
  764. int length = cipher.ProcessBytes(plaintext, 0, plaintext.Length, output, 0);
  765. cipher.DoFinal(output, length);
  766. return output;
  767. }
  768. }
  769. }