使用OpenFileDialog替代FolderBrowserDialog

1. 实现ShowOpenFileDialogForFolder方法:
   - 使用OpenFileDialog提供更现代的界面
   - 配置适合文件夹选择的参数
   - 通过文件选择来获取目录路径

2. 配置OpenFileDialog:
   - 设置CheckFileExists=false允许选择不存在的文件名
   - CheckPathExists=true确保路径存在
   - ValidateNames=false允许非标准文件名
   - 初始文件名设为选择文件夹提示用户

3. 路径处理逻辑:
   - 如果用户选择文件,返回文件所在目录
   - 处理边界情况和异常情况
   - 保持与原有功能的兼容性

4. 用户体验改进:
   - 更现代的对话框界面
   - 更好的文件导航体验
   - 支持地址栏直接输入

现在使用更现代的OpenFileDialog界面,用户体验更好!

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
yuuko 2025-11-24 16:35:39 +08:00
parent 9cc165014c
commit 91d45f9074

107
Form1.cs
View File

@ -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)