我有兩個TreeViews,每個都在驅動器上生成一個檔案夾結構。該程式只有 1 個在 2 個驅動器comboBox中構建。TreeViews我只使用一個comboBox,因為幾乎每個檔案夾都有相同的F:名稱Z:
我說幾乎是因為我有 3 個具有相似名稱但不確切名稱的檔案夾。
所以假設我的 DropDown 看起來像這樣:
Book1
Book2
Book3
Book4
所以 Z 上的目錄:看起來像上面的例子,因為那是我的來源comboBox
R:看起來像這樣:
Book1
Book2
Book3_projects_render
Book4
所以我的代碼適用于 Book1、Book2 和 Book4,但是當我在 DropDown 上單擊 Book3 并創建我的 TreeView 結構時,它將在 R 上創建一個新目錄:名為 Book3,我想要實作的解決方案是為目錄設定例外像Book3_projects_render這樣它不會創建一個新目錄。
我的代碼:
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();
}
uj5u.com熱心網友回復:
在創建目錄之前重命名 TreeNode 物件
這是一種根據Jimi 的建議重命名給定檔案夾的方法。
- 創建一個
Dictionary<string, string>以將目標檔案夾作為鍵,將新名稱作為值。 - 對于每個
TreeView控制元件,在臨時中克隆其節點TreeView以更改.Text目標節點的屬性,而不反映在主TreeView控制元件上。重命名父節點和/或子節點時,必須設定.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);
}
}
}
}
根據目的地重命名給定的 TreeNode 物件
密鑰取自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而不是硬代碼。
創建目錄后重命名目錄
您可以通過分別呼叫和類的.Move方法來重命名系統檔案或目錄。FileDirectory
// Rename a file...
File.Move("source", "destination");
// Rename a directory...
Directory.Move("source", "destination");
將選定的目錄映射到另一個
要將選定目錄從 映射ComboBox到另一個目錄,請根據目標(例如)目錄的位置更改destPathF或(不是兩者)。destPathZBook3_projects_render
例如:
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);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510006.html
標籤:C#表格组合框树视图
