using System.IO; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media.Imaging; using Microsoft.Win32; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Paddings; using Org.BouncyCastle.Crypto.Parameters; namespace ArchivesCenter3 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); updateinfo(); LoadFileContent(); levelitemupdate(); tagitemupdate(); Birthplaceitemupdate(); RelativeStatusitemupdate(); Load4Config(); MyCalendar.DisplayDate = DateTime.Today; MyCalendar.SelectedDate = DateTime.Today; Version version = Assembly.GetExecutingAssembly().GetName().Version; versionTextBlock.Text = $"版本号:{version.Major}.{version.Minor}.{version.Build}"; DateTime tomorrow = DateTime.Today.AddDays(1); // 创建一个从明天开始到最大日期的不可选日期范围 CalendarDateRange blackoutRange = new CalendarDateRange(tomorrow, DateTime.MaxValue); // 将该范围添加到 Calendar 的 BlackoutDates 集合中 MyCalendar.BlackoutDates.Add(blackoutRange); } private List lines; // 存储文件内容的列表 private int currentIndex = -1; // 当前行索引 private bool isNewRecord = false; // 标记是否是新建记录 private void updateinfo() { welcomeText.Text = "欢迎!" + Settings1.Default.Username; WelcomeTitle.Text = Settings1.Default.WelcomeTitle; DatabaseName.Text = Settings1.Default.DatabaseName; DatabaseSubtitle.Text = Settings1.Default.DatabaseSubtitle; UsernameCu.Text = Settings1.Default.Username; WelcomeTitleCu.Text = Settings1.Default.WelcomeTitle; DatabaseNameCu.Text = Settings1.Default.DatabaseName; DatabaseSubtitleCu.Text = Settings1.Default.DatabaseSubtitle; PasswordCu.Text = Settings1.Default.Password; DatabaseNameBox.Text = null; DatabaseSubtitleBox.Text = null; WelcomeTitleBox.Text = null; UsernameBox.Text = null; PasswordBox.Text = null; } private void Button_Click(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(DatabaseNameBox.Text)) Settings1.Default.DatabaseName = DatabaseNameBox.Text; if (!string.IsNullOrEmpty(DatabaseSubtitleBox.Text)) Settings1.Default.DatabaseSubtitle = DatabaseSubtitleBox.Text; if (!string.IsNullOrEmpty(WelcomeTitleBox.Text)) Settings1.Default.WelcomeTitle = WelcomeTitleBox.Text; if (!string.IsNullOrEmpty(UsernameBox.Text)) Settings1.Default.Username = UsernameBox.Text; if (!string.IsNullOrEmpty(PasswordBox.Text)) Settings1.Default.Password = PasswordBox.Text; Settings1.Default.Save(); updateinfo(); string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string levelConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "level.config"); string tagConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "tag.config"); string relativeStatusConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "relativeStatus.config"); string birthplaceConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "birthplace.config"); // 保存配置文件内容 File.WriteAllText(levelConfigPath, LevelConfigBox.Text); File.WriteAllText(tagConfigPath, TagConfigBox.Text); File.WriteAllText(relativeStatusConfigPath, RSConfigBox.Text); File.WriteAllText(birthplaceConfigPath, BPConfigBox.Text); } private void TextChanged(object sender, TextChangedEventArgs e) { bool allTextBoxesEmpty = string.IsNullOrWhiteSpace(DatabaseNameBox.Text) && string.IsNullOrWhiteSpace(DatabaseSubtitleBox.Text) && string.IsNullOrWhiteSpace(WelcomeTitleBox.Text) && string.IsNullOrWhiteSpace(UsernameBox.Text) && string.IsNullOrWhiteSpace(LevelConfigBox.Text) && string.IsNullOrWhiteSpace(TagConfigBox.Text) && string.IsNullOrWhiteSpace(RSConfigBox.Text) && string.IsNullOrWhiteSpace(BPConfigBox.Text) && string.IsNullOrWhiteSpace(PasswordBox.Text); settingSaveButton0.IsEnabled = !allTextBoxesEmpty; settingSaveButton1.IsEnabled = !allTextBoxesEmpty; } private void LoadFileContent() { // 获取用户文档文件夹路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // 构建目标文件路径 string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.info"); // 检查文件是否存在 if (File.Exists(filePath)) { // 读取文件内容 lines = File.ReadAllLines(filePath).ToList(); var people = new List(); foreach (var line in lines) { string[] parts = line.Split(new string[] { "**" }, StringSplitOptions.None); if (parts.Length > 26) // 确保有数据且排除图片路径 { people.Add(new Person { Name = parts.Length > 0 ? parts[0] : string.Empty, Gender = parts.Length > 1 ? parts[1] : string.Empty, BirthDate = parts.Length > 2 ? parts[2] : string.Empty, Level = parts.Length > 3 ? parts[3] : string.Empty, Tag = parts.Length > 4 ? parts[4] : string.Empty, IDType = parts.Length > 5 ? parts[5] : string.Empty, IDNumber = parts.Length > 6 ? parts[6] : string.Empty, Birthplace = parts.Length > 7 ? parts[7] : string.Empty, PoliticalStatus = parts.Length > 8 ? parts[8] : string.Empty, MaritalStatus = parts.Length > 9 ? parts[9] : string.Empty, Address = parts.Length > 10 ? parts[10] : string.Empty, FamilySituation = parts.Length > 11 ? parts[11] : string.Empty, AcquaintancePlace = parts.Length > 12 ? parts[12] : string.Empty, RelativeStatus = parts.Length > 13 ? parts[13] : string.Empty, Education = parts.Length > 14 ? parts[14] : string.Empty, PrimarySchool = parts.Length > 15 ? parts[15] : string.Empty, JuniorHighSchool = parts.Length > 16 ? parts[16] : string.Empty, HighSchool = parts.Length > 17 ? parts[17] : string.Empty, University = parts.Length > 18 ? parts[18] : string.Empty, GraduateSchool = parts.Length > 19 ? parts[19] : string.Empty, WorkUnit = parts.Length > 20 ? parts[20] : string.Empty, PhoneNumber = parts.Length > 21 ? parts[21] : string.Empty, Email = parts.Length > 22 ? parts[22] : string.Empty, WeChat = parts.Length > 23 ? parts[23] : string.Empty, QQ = parts.Length > 24 ? parts[24] : string.Empty, OtherContact = parts.Length > 25 ? parts[25] : string.Empty }); } } // 绑定数据到 DataGrid dataGrid.ItemsSource = people; } else { lines = new List(); dataGrid.ItemsSource = new List(); } } private void DisplayCurrentLine() { if (currentIndex >= 0 && currentIndex < lines.Count) { string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None); txtName.Text = parts.Length > 0 ? parts[0] : string.Empty; cmbGender.SelectedItem = GetComboBoxItem(cmbGender, parts.Length > 1 ? parts[1] : null); dpBirthDate.SelectedDate = parts.Length > 2 ? DateTime.Parse(parts[2]) : (DateTime?)null; cmbLevel.SelectedItem = GetComboBoxItem(cmbLevel, parts.Length > 3 ? parts[3] : null); cmbTag.SelectedItem = GetComboBoxItem(cmbTag, parts.Length > 4 ? parts[4] : null); cmbIDType.SelectedItem = GetComboBoxItem(cmbIDType, parts.Length > 5 ? parts[5] : null); txtIDNumber.Text = parts.Length > 6 ? parts[6] : string.Empty; cmbBirthplace.SelectedItem = GetComboBoxItem(cmbBirthplace, parts.Length > 7 ? parts[7] : null); cmbPoliticalStatus.SelectedItem = GetComboBoxItem(cmbPoliticalStatus, parts.Length > 8 ? parts[8] : null); cmbMaritalStatus.SelectedItem = GetComboBoxItem(cmbMaritalStatus, parts.Length > 9 ? parts[9] : null); txtAddress.Text = parts.Length > 10 ? parts[10] : string.Empty; txtFamilySituation.Text = parts.Length > 11 ? parts[11] : string.Empty; txtAcquaintancePlace.Text = parts.Length > 12 ? parts[12] : string.Empty; cmbRelativeStatus.SelectedItem = GetComboBoxItem(cmbRelativeStatus, parts.Length > 13 ? parts[13] : null); cmbEducation.SelectedItem = GetComboBoxItem(cmbEducation, parts.Length > 14 ? parts[14] : string.Empty); txtPrimarySchool.Text = parts.Length > 15 ? parts[15] : string.Empty; txtJuniorHighSchool.Text = parts.Length > 16 ? parts[16] : string.Empty; txtHighSchool.Text = parts.Length > 17 ? parts[17] : string.Empty; txtUniversity.Text = parts.Length > 18 ? parts[18] : string.Empty; txtGraduateSchool.Text = parts.Length > 19 ? parts[19] : string.Empty; txtWorkUnit.Text = parts.Length > 20 ? parts[20] : string.Empty; txtPhoneNumber.Text = parts.Length > 21 ? parts[21] : string.Empty; txtEmail.Text = parts.Length > 22 ? parts[22] : string.Empty; txtWeChat.Text = parts.Length > 23 ? parts[23] : string.Empty; txtQQ.Text = parts.Length > 24 ? parts[24] : string.Empty; txtOtherContact.Text = parts.Length > 25 ? parts[25] : string.Empty; // 更新图片显示控件 if (parts.Length > 26 && !string.IsNullOrEmpty(parts[26])) { try { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(parts[26], UriKind.Absolute); bitmap.EndInit(); imgPhoto.Source = bitmap; } catch { // 如果图片路径无效,清空图片控件 imgPhoto.Source = null; } } else { // 如果没有图片路径,清空图片控件 imgPhoto.Source = null; } } else { ClearControls(); } } private void ClearControls() { txtName.Clear(); cmbGender.SelectedItem = null; dpBirthDate.SelectedDate = new DateTime(1900, 1, 1); // 设置默认日期为 1900-01-01 dpBirthDate.Text = "1900/1/1"; cmbLevel.SelectedItem = null; cmbTag.SelectedItem = null; cmbIDType.SelectedItem = null; txtIDNumber.Clear(); cmbBirthplace.SelectedItem = null; cmbPoliticalStatus.SelectedItem = null; cmbMaritalStatus.SelectedItem = null; txtAddress.Clear(); txtFamilySituation.Clear(); txtAcquaintancePlace.Clear(); cmbRelativeStatus.SelectedItem = null; cmbEducation.SelectedItem = null; txtPrimarySchool.Clear(); txtJuniorHighSchool.Clear(); txtHighSchool.Clear(); txtUniversity.Clear(); txtGraduateSchool.Clear(); txtWorkUnit.Clear(); txtPhoneNumber.Clear(); txtEmail.Clear(); txtWeChat.Clear(); txtQQ.Clear(); txtOtherContact.Clear(); imgPhoto.Source = null; } private object GetComboBoxItem(ComboBox comboBox, string content) { if (string.IsNullOrEmpty(content)) return null; foreach (var item in comboBox.Items) { if (item.ToString() == content) { return item; } } return null; } private void PreviousButton_Click(object sender, RoutedEventArgs e) { if (currentIndex > 0) { currentIndex--; DisplayCurrentLine(); } } private void NextButton_Click(object sender, RoutedEventArgs e) { if (currentIndex < lines.Count - 1) { currentIndex++; DisplayCurrentLine(); } } private void SaveButton_Click(object sender, RoutedEventArgs e) { // 获取所有控件的值 string name = txtName.Text; string gender = cmbGender.SelectedItem?.ToString(); string birthDate = dpBirthDate.SelectedDate?.ToString("yyyy-MM-dd") ?? string.Empty; string level = cmbLevel.SelectedItem?.ToString(); string tag = cmbTag.SelectedItem?.ToString(); string idType = cmbIDType.SelectedItem?.ToString(); string idNumber = txtIDNumber.Text; string birthplace = cmbBirthplace.SelectedItem?.ToString(); string politicalStatus = cmbPoliticalStatus.SelectedItem?.ToString(); string maritalStatus = cmbMaritalStatus.SelectedItem?.ToString(); string address = txtAddress.Text; string familySituation = txtFamilySituation.Text; string acquaintancePlace = txtAcquaintancePlace.Text; string relativeStatus = cmbRelativeStatus.SelectedItem?.ToString(); string education = cmbEducation.SelectedItem?.ToString(); string primarySchool = txtPrimarySchool.Text; string juniorHighSchool = txtJuniorHighSchool.Text; string highSchool = txtHighSchool.Text; string university = txtUniversity.Text; string graduateSchool = txtGraduateSchool.Text; string workUnit = txtWorkUnit.Text; string phoneNumber = txtPhoneNumber.Text; string email = txtEmail.Text; string weChat = txtWeChat.Text; string qq = txtQQ.Text; string otherContact = txtOtherContact.Text; string photoPath = imgPhoto.Source?.ToString() ?? string.Empty; // 构建保存的内容,使用两个星号 ** 分割每一列 string content = string.Join("**", new string[] { name, gender, birthDate, level, tag, idType, idNumber, birthplace, politicalStatus, maritalStatus, address, familySituation, acquaintancePlace, relativeStatus, education, primarySchool, juniorHighSchool, highSchool, university, graduateSchool, workUnit, phoneNumber, email, weChat, qq, otherContact, photoPath }); // 获取用户文档文件夹路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // 构建目标文件路径 string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.info"); // 确保目标文件夹存在 string directoryPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } if (currentIndex >= 0 && currentIndex < lines.Count) { // 修改当前行的内容 lines[currentIndex] = content; } else { // 新增一行 lines.Add(content); } // 保存文件内容 File.WriteAllLines(filePath, lines); // 更新 DataGrid LoadFileContent(); MessageBox.Show("保存成功!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Information); } private void NewButton_Click(object sender, RoutedEventArgs e) { // 清空所有控件的内容 ClearControls(); // 如果是第一次点击新建按钮,添加一个空行 if (!isNewRecord) { lines.Add(string.Empty); isNewRecord = true; } // 设置当前索引为最后一行 currentIndex = lines.Count - 1; // 显示新行 DisplayCurrentLine(); } private void ChangePhotoButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Image Files (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png" }; if (openFileDialog.ShowDialog() == true) { // 获取用户文档文件夹路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // 构建目标文件夹路径 string targetDirectory = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people"); // 确保目标文件夹存在 if (!Directory.Exists(targetDirectory)) { Directory.CreateDirectory(targetDirectory); } // 生成随机文件名 string randomFileName = Guid.NewGuid().ToString() + Path.GetExtension(openFileDialog.FileName); string targetFilePath = Path.Combine(targetDirectory, randomFileName); // 复制文件到目标路径 File.Copy(openFileDialog.FileName, targetFilePath, true); // 更新图片显示控件 BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(targetFilePath, UriKind.Absolute); bitmap.EndInit(); imgPhoto.Source = bitmap; // 如果当前行存在,更新文件路径到 content.info 文件 if (currentIndex >= 0 && currentIndex < lines.Count) { string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None); if (parts.Length > 26) { parts[26] = targetFilePath; } else { parts = parts.Concat(new string[] { targetFilePath }).ToArray(); } lines[currentIndex] = string.Join("**", parts); SaveFileContent(); } } } private void SaveFileContent() { // 获取用户文档文件夹路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // 构建目标文件路径 string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.info"); // 确保目标文件夹存在 string directoryPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } // 保存文件内容 File.WriteAllLines(filePath, lines); } private void DeleteButton_Click(object sender, RoutedEventArgs e) { if (currentIndex >= 0 && currentIndex < lines.Count) { // 弹出确认对话框 MessageBoxResult result = MessageBox.Show("确定要删除当前记录吗?", "Archives Center", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None); // 检查是否有图片路径,并删除图片文件 if (parts.Length > 26 && !string.IsNullOrEmpty(parts[26])) { string photoPath = parts[26]; if (File.Exists(photoPath)) { File.Delete(photoPath); } } // 如果当前处于编辑状态,删除当前行 lines.RemoveAt(currentIndex); // 保存文件内容 SaveFileContent(); // 如果删除后还有行,显示上一行的内容 if (currentIndex > 0) { currentIndex--; DisplayCurrentLine(); } else { ClearControls(); } LoadFileContent(); } } else { // 如果当前处于新建状态,清空控件内容 ClearControls(); } } private void pSearchButton_Click(object sender, RoutedEventArgs e) { string searchContent = pSearchBox.Text.Trim(); // 获取搜索框中的内容 if (string.IsNullOrEmpty(searchContent)) { MessageBox.Show("请输入搜索内容!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } // 获取用户文档文件夹路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // 构建目标文件路径 string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.info"); if (File.Exists(filePath)) { List searchResults = new List(); string[] lines = File.ReadAllLines(filePath); foreach (var line in lines) { string[] parts = line.Split(new string[] { "**" }, StringSplitOptions.None); if (parts.Length > 26) // 确保有数据且排除图片路径 { // 检查每一列是否包含搜索内容 if (parts.Any(part => part.Contains(searchContent))) { searchResults.Add(new Person { Name = parts.Length > 0 ? parts[0] : string.Empty, Gender = parts.Length > 1 ? parts[1] : string.Empty, BirthDate = parts.Length > 2 ? parts[2] : string.Empty, Level = parts.Length > 3 ? parts[3] : string.Empty, Tag = parts.Length > 4 ? parts[4] : string.Empty, IDType = parts.Length > 5 ? parts[5] : string.Empty, IDNumber = parts.Length > 6 ? parts[6] : string.Empty, Birthplace = parts.Length > 7 ? parts[7] : string.Empty, PoliticalStatus = parts.Length > 8 ? parts[8] : string.Empty, MaritalStatus = parts.Length > 9 ? parts[9] : string.Empty, Address = parts.Length > 10 ? parts[10] : string.Empty, FamilySituation = parts.Length > 11 ? parts[11] : string.Empty, AcquaintancePlace = parts.Length > 12 ? parts[12] : string.Empty, RelativeStatus = parts.Length > 13 ? parts[13] : string.Empty, Education = parts.Length > 14 ? parts[14] : string.Empty, PrimarySchool = parts.Length > 15 ? parts[15] : string.Empty, JuniorHighSchool = parts.Length > 16 ? parts[16] : string.Empty, HighSchool = parts.Length > 17 ? parts[17] : string.Empty, University = parts.Length > 18 ? parts[18] : string.Empty, GraduateSchool = parts.Length > 19 ? parts[19] : string.Empty, WorkUnit = parts.Length > 20 ? parts[20] : string.Empty, PhoneNumber = parts.Length > 21 ? parts[21] : string.Empty, Email = parts.Length > 22 ? parts[22] : string.Empty, WeChat = parts.Length > 23 ? parts[23] : string.Empty, QQ = parts.Length > 24 ? parts[24] : string.Empty, OtherContact = parts.Length > 25 ? parts[25] : string.Empty }); } } } // 绑定搜索结果到 DataGrid pSearchdataGrid.ItemsSource = searchResults; } else { MessageBox.Show("未找到数据文件!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Error); } } private void LoadLevels_Click(object sender, RoutedEventArgs e) { levelitemupdate(); } private void levelitemupdate() { string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "config", "level.config"); string directoryPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } if (!File.Exists(filePath)) File.Create(filePath).Close(); string[] lines = File.ReadAllLines(filePath); cmbLevel.Items.Clear(); foreach (string line in lines) { cmbLevel.Items.Add(line); } } private void LoadTags_Click(object sender, RoutedEventArgs e) { tagitemupdate(); } private void tagitemupdate() { string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "config", "tag.config"); string directoryPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } if (!File.Exists(filePath)) File.Create(filePath).Close(); string[] lines = File.ReadAllLines(filePath); cmbTag.Items.Clear(); foreach (string line in lines) { cmbTag.Items.Add(line); } } private void LoadBirthplaces_Click(object sender, RoutedEventArgs e) { Birthplaceitemupdate(); } private void Birthplaceitemupdate() { string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "config", "birthplace.config"); string directoryPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } if (!File.Exists(filePath)) File.Create(filePath).Close(); string[] lines = File.ReadAllLines(filePath); cmbBirthplace.Items.Clear(); foreach (string line in lines) { cmbBirthplace.Items.Add(line); } } private void LoadRelativeStatuss_Click(object sender, RoutedEventArgs e) { RelativeStatusitemupdate(); } private void RelativeStatusitemupdate() { string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "config", "relativeStatus.config"); string directoryPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } if (!File.Exists(filePath)) File.Create(filePath).Close(); string[] lines = File.ReadAllLines(filePath); cmbRelativeStatus.Items.Clear(); foreach (string line in lines) { cmbRelativeStatus.Items.Add(line); } } private void Load4Config() { // 定义配置文件路径 string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string levelConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "level.config"); string tagConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "tag.config"); string relativeStatusConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "relativeStatus.config"); string birthplaceConfigPath = Path.Combine(baseDir, "ArchivesCenter", "config", "birthplace.config"); // 读取配置文件内容 LevelConfigBox.Text = File.Exists(levelConfigPath) ? File.ReadAllText(levelConfigPath) : string.Empty; TagConfigBox.Text = File.Exists(tagConfigPath) ? File.ReadAllText(tagConfigPath) : string.Empty; RSConfigBox.Text = File.Exists(relativeStatusConfigPath) ? File.ReadAllText(relativeStatusConfigPath) : string.Empty; BPConfigBox.Text = File.Exists(birthplaceConfigPath) ? File.ReadAllText(birthplaceConfigPath) : string.Empty; } private void MyCalendar_DisplayDateChanged(object sender, SelectionChangedEventArgs e) { // 获取用户文档库路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string archivesPath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "log"); // 获取选中的日期 DateTime selectedDate = MyCalendar.SelectedDate.Value; // 格式化日期为文件夹名称(例如:2025-02-13) string dateFolderName = selectedDate.ToString("yyyy-MM-dd"); string dateFolderPath = Path.Combine(archivesPath, dateFolderName); // 检查日期文件夹是否存在 if (Directory.Exists(dateFolderPath)) { // 如果存在,读取content.txt文件 string contentFilePath = Path.Combine(dateFolderPath, "content.txt"); if (File.Exists(contentFilePath)) { // 读取文件内容到RichTextBox string content = File.ReadAllText(contentFilePath); MyRichTextBox.Document.Blocks.Clear(); MyRichTextBox.Document.Blocks.Add(new Paragraph(new Run(content))); } } else { // 如果不存在,创建文件夹和content.txt文件 Directory.CreateDirectory(dateFolderPath); string contentFilePath = Path.Combine(dateFolderPath, "content.txt"); File.WriteAllText(contentFilePath, ""); // 创建空文件 MyRichTextBox.Document.Blocks.Clear(); } } private void LogSaveButton_Click(object sender, RoutedEventArgs e) { // 获取用户文档库路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string archivesPath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "log"); // 获取选中的日期 DateTime selectedDate = MyCalendar.SelectedDate.Value; // 格式化日期为文件夹名称(例如:2025-02-13) string dateFolderName = selectedDate.ToString("yyyy-MM-dd"); string dateFolderPath = Path.Combine(archivesPath, dateFolderName); // 获取content.txt文件路径 string contentFilePath = Path.Combine(dateFolderPath, "content.txt"); // 获取RichTextBox中的内容 TextRange textRange = new TextRange(MyRichTextBox.Document.ContentStart, MyRichTextBox.Document.ContentEnd); string content = textRange.Text; // 将内容写入文件 File.WriteAllText(contentFilePath, content); MessageBox.Show("保存成功!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Information); } private void LogDeleteButton_Click(object sender, RoutedEventArgs e) { // 获取用户文档库路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string archivesPath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "log"); // 获取选中的日期 DateTime selectedDate = MyCalendar.SelectedDate.Value; // 格式化日期为文件夹名称(例如:2025-02-13) string dateFolderName = selectedDate.ToString("yyyy-MM-dd"); string dateFolderPath = Path.Combine(archivesPath, dateFolderName); // 检查文件夹是否存在 if (Directory.Exists(dateFolderPath)) { // 删除文件夹及其内容 Directory.Delete(dateFolderPath, true); MyRichTextBox.Document.Blocks.Clear(); MessageBox.Show("文件夹已删除!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("该日期文件夹不存在!", "Archives Center", MessageBoxButton.OK, MessageBoxImage.Error); } } private void SearchButton_Click(object sender, RoutedEventArgs e) { // 获取用户文档库路径 string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string archivesPath = Path.Combine(userDocumentsPath, "ArchivesCenter", "Data", "Log"); // 获取搜索内容 string searchText = SearchTextBox.Text.Trim(); // 清空 DataGrid ResultDataGrid.ItemsSource = null; ResultDataGrid.Items.Clear(); // 如果搜索内容为空,直接返回 if (string.IsNullOrEmpty(searchText)) { MessageBox.Show("请输入搜索内容!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } // 遍历所有日期文件夹 try { List matchingDates = new List(); foreach (var dateFolder in Directory.GetDirectories(archivesPath)) { string contentFilePath = Path.Combine(dateFolder, "content.txt"); // 检查文件是否存在 if (File.Exists(contentFilePath)) { string content = File.ReadAllText(contentFilePath); // 检查内容是否包含搜索文本 if (content.Contains(searchText, StringComparison.OrdinalIgnoreCase)) { string dateFolderName = Path.GetFileName(dateFolder); matchingDates.Add(dateFolderName); } } } // 将匹配的日期显示到 DataGrid ResultDataGrid.ItemsSource = matchingDates; } catch (Exception ex) { MessageBox.Show($"搜索失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } private static byte[] key = Encoding.UTF8.GetBytes("8f4a3b5c6d7e9f1a2b3c4d5e6f7a8b9c"); private void Button_Click_2(object sender, RoutedEventArgs e) { string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string targetFolderPath = Path.Combine(documentsPath, "ArchivesCenter"); EncryptFolder(targetFolderPath); this.Close(); } public static void EncryptFolder(string folderPath) { foreach (string filePath in Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories)) { byte[] fileContent = File.ReadAllBytes(filePath); byte[] encryptedContent = Encrypt(fileContent); File.WriteAllBytes(filePath, encryptedContent); Console.WriteLine($"Encrypted: {filePath}"); } } private static readonly byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; private static byte[] Encrypt(byte[] plaintext) { var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new Pkcs7Padding()); var keyParam = new KeyParameter(key); var parameters = new ParametersWithIV(keyParam, iv); cipher.Init(true, parameters); byte[] output = new byte[cipher.GetOutputSize(plaintext.Length)]; int length = cipher.ProcessBytes(plaintext, 0, plaintext.Length, output, 0); cipher.DoFinal(output, length); return output; } } }