复制文件夹到剪贴板

3
我在这次尝试中使用C#作为编程语言。
我在无数论坛和其他Google搜索结果中寻找答案,但是我找不到我的问题的解决方案。
我有一个FileExplorer并且我在我的ContextMenuStrip组件中有复制/粘贴/删除菜单项。现在,我已经能够复制文件在我的File Explorer中,但是我正在尝试弄清楚如何复制文件夹。
我使用TreeView组件作为我的主要组件,并将其绑定到此操作。
什么是文件管理器?这是我所说的内容(这是我的文件资源管理器的实际图像): enter image description here 这是我当前用于在“FileExplorer\”文件夹内部复制“文件”的代码。它还检索'FileExplorer\'文件夹中的其他文件夹/文件。
    private void toolStripMenuItemCopy_Click(object sender, EventArgs e)
    {
        try
        {
            DirectoryInfo[] directories = directoryInfo.GetDirectories();
            foreach (FileInfo file in directoryInfo.GetFiles()) // Retrieving the files inside of FileExplorer\ folder
            {
                if (file.Exists && file.Name == treeView.SelectedNode.Text)
                {
                    StringCollection filePath = new StringCollection();
                    filePath.Add(file.FullName);
                    Clipboard.SetFileDropList(filePath); // Copying the selected (node) file
                }
            }

            if (directories.Length > 0)
            {
                foreach (DirectoryInfo directory in directories) // Retrieving the directories inside of the FileExplorer\ folder
                {
                    foreach (FileInfo file in directory.GetFiles()) // Retreiving all the files inside of the directories
                        if (file.Exists && file.Name == treeView.SelectedNode.Text)
                        {
                            StringCollection filePath = new StringCollection();
                            filePath.Add(file.FullName);
                            Clipboard.SetFileDropList(filePath); // Copying the selected (node) file
                        }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

如果有人能给我提供关于如何在文件资源管理器中复制文件夹的指针/代码,那将不胜感激!

2个回答

2

VB.NET

Dim f() As String = {"C:\SureFire\TWHomepage"}
Dim d As New DataObject(DataFormats.FileDrop, f)
Clipboard.SetDataObject(d, True)

0
StringCollection files = Clipboard.GetFileDropList();
foreach (string file in files)
{
    if (System.IO.Directory.Exists(file))
    {
        string destPath = info.FullName;
        FileSystem.CopyDirectory(file, destPath, UIOption.AllDialogs, UICancelOption.DoNothing);

    }
}

请添加一些说明。解释为什么你的代码应该有效。 - Nilambar Sharma
不知道如何解释。但让我们试试。在原始代码中,文件和目录的路径被放置在剪贴板上。问题是“如何复制文件夹”,代码示例分析剪贴板上的所有路径,选择文件夹并将其复制到目标路径。在这种情况下,您需要自行负责正确的目标路径。 - Volodymyr Korobejnik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接