Rename the TreeNode objects Before creating the Directories
Here's a way to rename given folders based on Jimi's suggestion.
- Create a
Dictionary<string, string> to have the target folders as keys and the new names as values.
- For each
TreeView control, clone its nodes in a temp TreeView to change the .Text properties of the target nodes without reflecting that on the main TreeView controls. Setting the .Text property is necessary to get the correct .FullPath property when you rename a parent and/or child node.
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
The keys are taken from the ComboBox control.
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);
}
}
}
You can add a control such as DataGridView to get the key-value pairs of the dirsToRename dictionary instead of the hard-code.
Rename a Directory after creating it
You can rename a system file or directory by calling the .Move method of the File and Directory classes respectively.
// Rename a file...
File.Move("source", "destination");
// Rename a directory...
Directory.Move("source", "destination");
Map a selected Directory to another
To map a selected directory from the ComboBox to another directory, change destPathF or destPathZ (not both) depending on the location of the target (Book3_projects_render for example) directory.
For example:
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);
}
}
}