MainWindow.xaml.cs 37 KB

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