Rename the TreeNode objects Before creating the Directories个
这里有一个根据Jimi's条建议重命名给定文件夹的方法.
- 创建
Dictionary<string, string>
以将目标文件夹作为关键字,并将新名称作为值.
- 对于每个
TreeView
个控件,在临时TreeView
中克隆其 node 以更改目标 node 的.Text
个属性,而不将其反映在主TreeView
个控件上.重命名父 node 和/或子 node 时,必须设置.Text
属性才能获得正确的.FullPath
属性.
private void SomeButton_Click(object sender, EventArgs e)
{
var driveF = "C:\\";
var driveZ = "D:\\";
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>();
var dirsToRename = new Dictionary<string, string>
{
{ "Book 4", "Book 1" },
{ "Book 5", "Book 2" },
{ "Book 6", "Book 3" },
{ "Books", "Books 123" }
};
using (var tv = new TreeView())
{
tv.Nodes.AddRange(pathLorem.Nodes
.OfType<TreeNode>()
.Select(n => n.Clone() as TreeNode)
.ToArray());
foreach (var node in GetCheckedNodes(tv.Nodes))
{
if (dirsToRename.ContainsKey(node.Text)) node.Text = dirsToRename[node.Text];
var sPath = Path.Combine(destPathF, node.FullPath.Replace(treeSep, dirSep));
Directory.CreateDirectory(sPath);
if (node.Level == 0) shortcuts.Add(sPath.TrimStart(driveF.ToArray()));
}
tv.Nodes.Clear();
tv.Nodes.AddRange(ipsumPath.Nodes
.OfType<TreeNode>()
.Select(n => n.Clone() as TreeNode)
.ToArray());
foreach (var node in GetCheckedNodes(tv.Nodes))
{
if (dirsToRename.ContainsKey(node.Text)) node.Text = dirsToRename[node.Text];
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);
}
}
}
}
Rename given TreeNode objects based on destinations个
keys 取自ComboBox
控制键.
private void btnTest_Click(object sender, EventArgs e)
{
var driveF = "C:\\";
var driveZ = "D:\\";
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>();
var dirsToRename = new Dictionary<string, string>
{
{ "Book 1", "Book 4" },
{ "Book 2", "Book 5" },
{ "Book 3", "Book 6" }
};
foreach (var node in GetCheckedNodes(pathLorem.Nodes))
{
var sPath = Path.Combine(destPathF, node.FullPath.Replace(treeSep, dirSep));
// Comment this if `pathLorem` is not the source of the keys...
if (dirsToRename.ContainsKey(node.Text))
sPath = Path.Combine(Path.GetDirectoryName(sPath), dirsToRename[node.Text]);
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));
// Comment this if `ipsumPath` is not the source of the keys...
if (dirsToRename.ContainsKey(node.Text))
sPath = Path.Combine(Path.GetDirectoryName(sPath), dirsToRename[node.Text]);
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);
}
}
}
您可以添加一个像DataGridView
这样的控件来获得dirsToRename
字典的键-值对,而不是硬编码.
Rename a Directory after creating it个
您可以通过分别调用File
和Directory
类的.Move
方法来重命名系统文件或目录.
// Rename a file...
File.Move("source", "destination");
// Rename a directory...
Directory.Move("source", "destination");
Map a selected Directory to another个
要将所选目录从ComboBox
映射到另一个目录,请根据目标目录(例如Book3_projects_render
)的位置更改destPathF
或destPathZ
(不是两者).
例如:
private void SomeButton_Click(object sender, EventArgs e)
{
var driveF = "C:\\";
var driveZ = "D:\\";
var selDirInfo = loremDropDown.SelectedItem as DirectoryInfo;
var selDir = selDirInfo.FullName;
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>();
var exDir = "Book 3";
var repDir = "Book3_projects_render";
bool isExDir = selDirInfo.Name == exDir;
if (isExDir)
{
// If `Book3_projects_render` is on F: otherwise comment...
//destPathF = Path.Combine(Path.GetDirectoryName(destPathF), "Book3_projects_render");
// If `Book3_projects_render` is on Z: otherwise comment...
destPathZ = Path.Combine(Path.GetDirectoryName(destPathZ), repDir);
}
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.Substring(driveF.Length));
}
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.Substring(driveZ.Length));
}
foreach (var shortcut in shortcuts)
{
var dirF = $"{driveF}{shortcut}";
var dirZ = $"{driveZ}{shortcut}";
if (isExDir)
{
// If `Book3_projects_render` is on Z: otherwise switch exDir and repDir...
dirF = dirF.Replace($@"\{repDir}\", $@"\{exDir}\");
dirZ = dirZ.Replace($@"\{exDir}\", $@"\{repDir}\");
}
if (Directory.Exists(dirF) && Directory.Exists(dirZ))
{
CreateShortcut(dirF, dirZ);
CreateShortcut(dirZ, dirF);
}
}
}