2013-03-07 244 views
1

我使用C#作为此试用版的编程语言。将文件夹复制到剪贴板

我搜遍了无数的论坛和其他地方,我的谷歌搜索结果弹出。但是,我找不到解决我的问题。

我有一个FileExplorer,我有我的上下文菜单条组件中的菜单项复制/粘贴/删除。现在,我有我的文件资源管理器中的文件复制工作,但我试图找出如何复制文件夹。

我使用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); 
     } 
    } 

帮助将不胜感激,如果有人可以给我所需的指针/代码如何复制我的文件资源管理器内的文件夹!

回答

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

    } 
} 
+0

请添加一些解释。解释为什么你的代码应该工作。 – Nilambar 2015-07-10 06:26:20

+0

不知道该如何解释。但我们试试吧。在文件和目录的原始代码路径放置在剪贴板上。问题是“如何处理文件夹”,代码示例分析剪贴板上的所有路径,选择文件夹并将文件夹复制到目标路径。在这种情况下,您自己负责正确的目的地路径。 – 2015-07-13 06:44:46

1

VB.NET

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