双驱动器TreeView文件夹生成:例外同名文件夹处理需求
问题描述
我有两个TreeView控件,分别用于生成F:和Z:驱动器的文件夹结构,程序仅使用一个ComboBox来构建这两个TreeView——因为两个驱动器上的文件夹名称基本一致。但存在一处例外:
- ComboBox的数据源是Z:的文件夹,包含
Book1、Book2、Book3、Book4 - F:上的对应文件夹为
Book1、Book2、Book3_projects_render、Book4
当前代码处理Book1、Book2、Book4时运行正常,但选择下拉框中的Book3时,会在F:上新建Book3文件夹,我需要添加例外处理逻辑,避免创建这个不必要的新文件夹。
附原代码:
public Form1() { InitializeComponent(); // ... loremDropDown.DisplayMember = "Name"; loremDropDown.ValueMember = "FullName"; loremDropDown.DataSource = new DirectoryInfo("F:\\\\").GetDirectories(); } private void SomeButton_Click(object sender, EventArgs e) { var driveF = "F:\\\\"; var driveZ = "Z:\\\\"; var selDir = loremDropDown.SelectedValue.ToString(); var destPathF = selDir.Replace(Path.GetPathRoot(selDir), driveF); var destPathZ = selDir.Replace(Path.GetPathRoot(selDir), driveZ); var treeSep = pathLorem.PathSeparator; var dirSep = Path.DirectorySeparatorChar.ToString(); var shortcuts = new HashSet<string>(); foreach (var node in GetCheckedNodes(pathLorem.Nodes)) { var sPath = Path.Combine(destPathF, node.FullPath.Replace(treeSep, dirSep)); Directory.CreateDirectory(sPath); if (node.Level == 0) shortcuts.Add(sPath.TrimStart(driveF.ToArray())); } foreach (var node in GetCheckedNodes(ipsumPath.Nodes)) { var sPath = Path.Combine(destPathZ, node.FullPath.Replace(treeSep, dirSep)); Directory.CreateDirectory(sPath); if (node.Level == 0) shortcuts.Add(sPath.TrimStart(driveZ.ToArray())); } foreach (var shortcut in shortcuts) { var dirF = $"{driveF}{shortcut}"; var dirZ = $"{driveZ}{shortcut}"; if (Directory.Exists(dirF) && Directory.Exists(dirZ)) { CreateShortcut(dirF, dirZ); CreateShortcut(dirZ, dirF); } } } private void CreateShortcut(string shortcutPath, string targetPath) { WshShell wshShell = new WshShell(); string fileName = Path.Combine(shortcutPath, $"{Application.ProductName}.lnk"); IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(fileName); shortcut.TargetPath = targetPath; shortcut.Save(); }
解决方案
通过建立例外文件夹名称映射表,在生成目标路径时自动替换例外名称,避免创建错误文件夹。具体修改如下:
- 定义例外映射字典:集中管理名称不一致的文件夹,后续新增例外只需添加键值对
- 调整目标路径生成逻辑:处理F:侧路径时,自动替换例外名称
- 修正快捷方式匹配逻辑:确保快捷方式关联到正确的映射文件夹
修改后的完整代码:
public Form1() { InitializeComponent(); // ... loremDropDown.DisplayMember = "Name"; loremDropDown.ValueMember = "FullName"; loremDropDown.DataSource = new DirectoryInfo("F:\\\\").GetDirectories(); } private void SomeButton_Click(object sender, EventArgs e) { var driveF = "F:\\\\"; var driveZ = "Z:\\\\"; var selDir = loremDropDown.SelectedValue.ToString(); var selectedDirName = new DirectoryInfo(selDir).Name; // 定义例外文件夹映射:Z侧名称 -> F侧对应名称 var folderNameMappings = new Dictionary<string, string> { {"Book3", "Book3_projects_render"} }; // 处理F侧目标路径,替换例外名称 string destPathF; if (folderNameMappings.TryGetValue(selectedDirName, out var mappedName)) { destPathF = Path.Combine(driveF, mappedName); } else { destPathF = selDir.Replace(Path.GetPathRoot(selDir), driveF); } var destPathZ = selDir.Replace(Path.GetPathRoot(selDir), driveZ); var treeSep = pathLorem.PathSeparator; var dirSep = Path.DirectorySeparatorChar.ToString(); var shortcuts = new HashSet<string>(); foreach (var node in GetCheckedNodes(pathLorem.Nodes)) { var sPath = Path.Combine(destPathF, node.FullPath.Replace(treeSep, dirSep)); Directory.CreateDirectory(sPath); if (node.Level == 0) { var shortcutKey = sPath.TrimStart(driveF.ToArray()); // 如果是根节点且属于例外,存储映射后的名称用于快捷方式匹配 if (folderNameMappings.TryGetValue(selectedDirName, out _)) { shortcutKey = mappedName + shortcutKey.Substring(selectedDirName.Length); } shortcuts.Add(shortcutKey); } } foreach (var node in GetCheckedNodes(ipsumPath.Nodes)) { var sPath = Path.Combine(destPathZ, node.FullPath.Replace(treeSep, dirSep)); Directory.CreateDirectory(sPath); if (node.Level == 0) shortcuts.Add(sPath.TrimStart(driveZ.ToArray())); } foreach (var shortcut in shortcuts) { var dirF = Path.Combine(driveF, folderNameMappings.TryGetValue(shortcut.Split(dirSep)[0], out var map) ? map : shortcut); var dirZ = Path.Combine(driveZ, shortcut); if (Directory.Exists(dirF) && Directory.Exists(dirZ)) { CreateShortcut(dirF, dirZ); CreateShortcut(dirZ, dirF); } } } private void CreateShortcut(string shortcutPath, string targetPath) { WshShell wshShell = new WshShell(); string fileName = Path.Combine(shortcutPath, $"{Application.ProductName}.lnk"); IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(fileName); shortcut.TargetPath = targetPath; shortcut.Save(); }
关键修改说明
- 例外映射字典:
folderNameMappings统一管理所有名称不一致的文件夹,后续新增例外无需修改核心逻辑 - 目标路径替换:生成F侧根路径时,自动将
Book3替换为Book3_projects_render,避免创建错误文件夹 - 快捷方式匹配修正:确保快捷方式能正确关联到映射后的文件夹,而非原名称文件夹
内容的提问来源于stack exchange,提问作者TheCodingTentacle
相关产品推荐
相关产品推荐

