Reputation: 65
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):
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!
Upvotes: 3
Views: 2440
Reputation: 1591
VB.NET
Dim f() As String = {"C:\SureFire\TWHomepage"}
Dim d As New DataObject(DataFormats.FileDrop, f)
Clipboard.SetDataObject(d, True)
Upvotes: 2
Reputation: 36
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);
}
}
Upvotes: 0