做一个小软件,类似于资源管理器的操作形式,剪切,复制,粘贴自然是少不了的了,在MSDN中找来好久,总算是明白过来了,代码如下:
/// <summary>
/// 复制或剪切文件至剪贴板(方法)
/// </summary>
/// <param name="files">需要添加到剪切板的文件路径数组</param>
/// <param name="cut">是否剪切true为剪切,false为复制</param>
public static void CopyToClipboard(string[] files, bool cut)
{
if (files == null) return;
IDataObject data = new DataObject(DataFormats.FileDrop, files);
MemoryStream memo = new MemoryStream(4);
byte[] bytes = new byte[] { (byte)(cut ? 2 : 5), 0, 0, 0 };
memo.Write(bytes, 0, bytes.Length);
data.SetData("Preferred DropEffect", memo);
Clipboard.SetDataObject(data);
}
/// <summary>
/// 获取剪贴板中的文件列表(方法)
/// </summary>
/// <returns>System.Collections.List<string>返回剪切板中文件路径集合</returns>
public static List<string> GetClipboardList()
{
List<string> clipboardList=new List<string>();
System.Collections.Specialized.StringCollection sc = Clipboard.GetFileDropList();
for (int i = 0; i < sc.Count; i++)
{
string listfileName = sc[i];
clipboardList.Add(listfileName);
}
return clipboardList;
}