简体   繁体   English

在列表视图的上下文菜单中实现复制和粘贴

[英]implementing copy and paste in context menu of a listview

I have a ListView that shows files and folders of a computer. 我有一个ListView ,它显示计算机的文件和文件夹。 I have added a context menu that, when right clicked, it can copy/paste/delete/rename etc. When the file is selected its path is saved in a string. 我添加了一个上下文菜单,右键单击该菜单可以复制/粘贴/删除/重命名等。选择文件后,其路径保存在字符串中。

Now how can I cut that and save to clipboard. 现在我该如何剪切并保存到剪贴板。 and then going to a directory and paste the file there. 然后转到目录并将文件粘贴到该目录。 Copy and move to methods don't seem to work as I have to implement the copy and paste on separate clicks 复制并移动到方法似乎不起作用,因为我必须在单独的单击上实施复制和粘贴

 private void copyToolStripMenuItem_Click(object sender, EventArgs e)
 {
     string ItemClicked = listView1.FocusedItem.Tag.ToString();
     if (ItemClicked != string.Empty)
     {
         Clipboard.SetFileDropList(ItemClicked);//error
     }
}

You need to pass in a StringCollection not a string . 您需要传递StringCollection而不是string Try this: 尝试这个:

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (listView1.SelectedItems.Count > 0)
    {
        System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
        sc.Add(listView1.FocusedItem.Tag.ToString());

        Clipboard.SetFileDropList(sc);
    }
}

Note though, this will only COPY to the clipboard. 但是请注意,这只会复制到剪贴板。 Inorder to cut like you want you'll need to decide what that means: delete from the listview or delete the file from its original location (after pasting?) 为了按照自己的意愿进行剪切,您需要确定这意味着什么:从listview删除或从其原始位置删除文件(粘贴之后?)


In response to the comments: 针对评论:

//public variables
StringCollection copiedFiles = new StringCollection();
bool cutWasSelected;

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
    CopySelectedFiles();
    cutWasSelected = false;
}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
    CopySelectedFiles();
    cutWasSelected = true;
}
private void CopySelectedFiles()
{
    if (listView1.SelectedItems.Count > 0)
    {
        foreach (ListViewItem item in listView1.SelectedItems)
        {
            copiedFiles.Add(item.Tag.ToString());
        }
    }
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
    string destinationFolder;//however you select this
    PasteCopiedFiles(destinationFolder, cutWasSelected);

}

private void PasteCopiedFiles(string DestinationFolder, bool deleteSourceFiles)
{
    if (copiedFiles.Count > 0)
    {
        foreach (string file in copiedFiles)
        {
            if (deleteSourceFiles)
            {
                File.Move(file,Path.Combine(new string[]{DestinationFolder,Path.GetFileName(file)}));
            }
            else
            {
                File.Copy(file, Path.Combine(new string[] { DestinationFolder, Path.GetFileName(file) }));
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM