我有两个TreeViews,每个都在驱动器上生成一个文件夹 struct . 该计划只有1 comboBox个,可以在2个驱动器中构建TreeViews个驱动器. 我只使用一个comboBox,因为几乎每个文件夹在F:Z:上都有相同的名称

我说几乎是因为我有三个文件夹,它们的名字相似,但不完全相同.

假设我的下拉列表如下所示:

Book1
Book2
Book3
Book4

Z:上的目录类似于上面的例子,因为这是我comboBox的来源

R:看起来是这样的:

Book1
Book2
Book3_projects_render
Book4

所以我的代码适用于Book1,Book2和Book4,但当我点击下拉菜单上的Book3并创建我的TreeView struct 时,它将在R:命名为Book3上创建一个新的目录,我想要实现的解决方案是对像Book3_projects_render这样的目录进行例外处理,这样它就不会创建新的目录.

My Code:

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();
}

推荐答案

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

您可以通过分别调用FileDirectory类的.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)的位置更改destPathFdestPathZ(不是两者).

例如:

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);
        }
    }
}

Csharp相关问答推荐

MongoDB将JS查询转换为C#的问题

编写DataAnnotations自定义验证器的多种方法

. NET在上一个操作完成之前,在此上下文实例上启动了第二个操作

C++/C#HostFXR通过std::tuple传递参数

Polly使用泛型重试和重试包装函数

使页面内容居中

查找表中的模式

ASP.NET Core AutoMapper:如何解决错误 CS0121调用在以下方法或属性之间不明确

如何使用C#中的主构造函数功能使用多个构造函数?

如何在ASP.NET Core8中启用REST应用程序的序列化?

如何比较C#中的L和ł(波兰字符)返回TRUE

.NET:从XPath定位原始XML文档中的 node

关于扩展文件类C#的矛盾

CRL已过期,但ChainStatus告诉我RevocationStatus未知

如何在mediatr命令中访问HttpContext而不安装弃用的nuget包

未在Windows上运行的Maui项目

为什么我的用户界面对象移动到略低于实际目标?

如何在绑定到数据库的datagridview中向上或向下移动行

在使用.NET EF Core DbContext属性之前,是否应使用null判断

如何对列表<;列表>;使用集合表达式?