using System.IO; using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using Microsoft.Win32; namespace ArchivesCenter3 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); updateinfo(); LoadFileContent(); Version version = Assembly.GetExecutingAssembly().GetName().Version; versionTextBlock.Text = $"版本号:{version.Major}.{version.Minor}.{version.Build}"; } 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(); } 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(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.txt"); // 检查文件是否存在 if (File.Exists(filePath)) { // 读取文件内容 lines = File.ReadAllLines(filePath).ToList(); } else { lines = 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] : null); 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.txt"); // 确保目标文件夹存在 string directoryPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } if (currentIndex >= 0 && currentIndex < lines.Count) { // 修改当前行的内容 lines[currentIndex] = content; isNewRecord = false; // 重置新建记录标志 } else { // 新增一行 lines.Add(content); } // 保存文件内容 File.WriteAllLines(filePath, lines); MessageBox.Show("保存成功!"); } 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.txt 文件 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.txt"); // 确保目标文件夹存在 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) { // 如果当前处于编辑状态,删除当前行 lines.RemoveAt(currentIndex); // 保存文件内容 SaveFileContent(); // 如果删除后还有行,显示上一行的内容 if (currentIndex > 0) { currentIndex--; DisplayCurrentLine(); } else { ClearControls(); } } else { // 如果当前处于新建状态,清空控件内容 ClearControls(); } } } }