diff --git a/Form1.cs b/Form1.cs index 056b1a5..aeb241b 100644 --- a/Form1.cs +++ b/Form1.cs @@ -31,86 +31,75 @@ namespace SlideCombine private void btnBrowseSource_Click(object sender, EventArgs e) { - using (FolderBrowserDialog dialog = new FolderBrowserDialog()) + string selectedPath = ShowOpenFileDialogForFolder("请选择包含PDF文件夹的路径", txtSourcePath.Text); + if (!string.IsNullOrEmpty(selectedPath)) { - dialog.Description = "请选择包含PDF文件夹的路径"; - dialog.ShowNewFolderButton = true; - dialog.RootFolder = Environment.SpecialFolder.MyComputer; - - if (!string.IsNullOrEmpty(txtSourcePath.Text)) - { - try - { - dialog.SelectedPath = txtSourcePath.Text; - } - catch - { - // 如果路径无效,使用默认路径 - } - } - - if (dialog.ShowDialog() == DialogResult.OK) - { - txtSourcePath.Text = dialog.SelectedPath; - LogInfo($"已选择PDF路径: {dialog.SelectedPath}"); - } + txtSourcePath.Text = selectedPath; + LogInfo($"已选择PDF路径: {selectedPath}"); } } private void btnBrowseText_Click(object sender, EventArgs e) { - using (FolderBrowserDialog dialog = new FolderBrowserDialog()) + string selectedPath = ShowOpenFileDialogForFolder("请选择包含元数据TXT文件的路径", txtTextPath.Text); + if (!string.IsNullOrEmpty(selectedPath)) { - dialog.Description = "请选择包含元数据TXT文件的路径"; - dialog.ShowNewFolderButton = true; - dialog.RootFolder = Environment.SpecialFolder.MyComputer; - - if (!string.IsNullOrEmpty(txtTextPath.Text)) - { - try - { - dialog.SelectedPath = txtTextPath.Text; - } - catch - { - // 如果路径无效,使用默认路径 - } - } - - if (dialog.ShowDialog() == DialogResult.OK) - { - txtTextPath.Text = dialog.SelectedPath; - LogInfo($"已选择TXT源路径: {dialog.SelectedPath}"); - } + txtTextPath.Text = selectedPath; + LogInfo($"已选择TXT源路径: {selectedPath}"); } } private void btnBrowseOutput_Click(object sender, EventArgs e) { - using (FolderBrowserDialog dialog = new FolderBrowserDialog()) + string selectedPath = ShowOpenFileDialogForFolder("请选择合并后TXT文件的输出路径", txtOutputPath.Text); + if (!string.IsNullOrEmpty(selectedPath)) { - dialog.Description = "请选择合并后TXT文件的输出路径"; - dialog.ShowNewFolderButton = true; - dialog.RootFolder = Environment.SpecialFolder.MyComputer; + txtOutputPath.Text = selectedPath; + LogInfo($"已选择最终输出路径: {selectedPath}"); + } + } - if (!string.IsNullOrEmpty(txtOutputPath.Text)) + private string ShowOpenFileDialogForFolder(string title, string initialPath) + { + using (OpenFileDialog dialog = new OpenFileDialog()) + { + dialog.Title = title; + dialog.Filter = "文件夹|*.|所有文件|*.*"; + dialog.FileName = "选择文件夹"; + dialog.CheckFileExists = false; + dialog.CheckPathExists = true; + dialog.Multiselect = false; + dialog.ValidateNames = false; + + // 设置初始目录 + if (!string.IsNullOrEmpty(initialPath) && Directory.Exists(initialPath)) { - try - { - dialog.SelectedPath = txtOutputPath.Text; - } - catch - { - // 如果路径无效,使用默认路径 - } + dialog.InitialDirectory = initialPath; + } + else + { + dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); } if (dialog.ShowDialog() == DialogResult.OK) { - txtOutputPath.Text = dialog.SelectedPath; - LogInfo($"已选择最终输出路径: {dialog.SelectedPath}"); + string selectedPath = Path.GetDirectoryName(dialog.FileName); + + // 如果用户选择了文件,返回文件所在目录 + if (!string.IsNullOrEmpty(selectedPath)) + { + return selectedPath; + } + + // 如果用户只选择了目录(这可能在某些情况下发生) + if (Directory.Exists(dialog.FileName)) + { + return dialog.FileName; + } } } + + return null; } private void btnClear_Click(object sender, EventArgs e)