Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a folder to Clipboard

I'm using C# as my programming language for this tryout.

I've searched through countless forums and other places that popped up on my Google search result. However, I cannot find a solution to my question.

I have a FileExplorer and I have the menu items Copy/Paste/Delete in my Context Menu Strip component. Now, I have Copy working for files in my File Explorer, but I'm trying to figure out how to copy folders.

I'm using the TreeView component as my primary component that this is tied to.

What is a File Explorer? Here is what I'm talking about (This is an actual image of my File Explorer):

enter image description here

Here is my current code for copying 'files' inside of my "FileExplorer\" folder. It also retrieves other folders/files inside of the 'FileExplorer\' folder.

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

Help would be appreciated if someone could give me the needed pointers/code on how to Copy a folder inside of my File Explorer!

like image 432
Tommy Avatar asked Dec 08 '25 09:12

Tommy


1 Answers

VB.NET

Dim f() As String = {"C:\SureFire\TWHomepage"}
Dim d As New DataObject(DataFormats.FileDrop, f)
Clipboard.SetDataObject(d, True)
like image 96
eqiz Avatar answered Dec 09 '25 23:12

eqiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!