使用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

101
Form1.cs
View File

@ -31,88 +31,77 @@ namespace SlideCombine
private void btnBrowseSource_Click(object sender, EventArgs e) 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文件夹的路径"; txtSourcePath.Text = selectedPath;
dialog.ShowNewFolderButton = true; LogInfo($"已选择PDF路径: {selectedPath}");
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}");
}
} }
} }
private void btnBrowseText_Click(object sender, EventArgs e) 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文件的路径"; txtTextPath.Text = selectedPath;
dialog.ShowNewFolderButton = true; LogInfo($"已选择TXT源路径: {selectedPath}");
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}");
}
} }
} }
private void btnBrowseOutput_Click(object sender, EventArgs e) 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文件的输出路径"; txtOutputPath.Text = selectedPath;
dialog.ShowNewFolderButton = true; LogInfo($"已选择最终输出路径: {selectedPath}");
dialog.RootFolder = Environment.SpecialFolder.MyComputer; }
}
if (!string.IsNullOrEmpty(txtOutputPath.Text)) private string ShowOpenFileDialogForFolder(string title, string initialPath)
{ {
try using (OpenFileDialog dialog = new OpenFileDialog())
{ {
dialog.SelectedPath = txtOutputPath.Text; 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))
{
dialog.InitialDirectory = initialPath;
} }
catch else
{ {
// 如果路径无效,使用默认路径 dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
}
} }
if (dialog.ShowDialog() == DialogResult.OK) if (dialog.ShowDialog() == DialogResult.OK)
{ {
txtOutputPath.Text = dialog.SelectedPath; string selectedPath = Path.GetDirectoryName(dialog.FileName);
LogInfo($"已选择最终输出路径: {dialog.SelectedPath}");
// 如果用户选择了文件,返回文件所在目录
if (!string.IsNullOrEmpty(selectedPath))
{
return selectedPath;
}
// 如果用户只选择了目录(这可能在某些情况下发生)
if (Directory.Exists(dialog.FileName))
{
return dialog.FileName;
} }
} }
} }
return null;
}
private void btnClear_Click(object sender, EventArgs e) private void btnClear_Click(object sender, EventArgs e)
{ {
txtSourcePath.Clear(); txtSourcePath.Clear();