您需要传入 a StringCollection
not a string
。尝试这个:
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);
}
}
但请注意,这只会复制到剪贴板。为了像您想要的那样剪切,您需要确定这意味着什么:listview
从原始位置删除或从其原始位置删除文件(粘贴后?)
回应评论:
//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) }));
}
}
}
}