Cut, copy and paste of WinForm / WPF clipboard
Winform
/// <summary>
///Copy or cut files to clipboard (method)
/// </summary>
///< param name = "files" > file path array to be added to the clipboard < / param >
///< param name = "cut" > whether to cut true means to cut and false means to copy < / 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>
///Get the list of files in the clipboard (method)
/// </summary>
/// <returns>System. Collections. List < string > returns the set of file paths in the clipboard < / returns >
public static List<string> GetClipboardList()
{
List<string> clipboardList = new List<string>();
IDataObject dataObject = Clipboard.GetDataObject();
if (dataObject.GetDataPresent(DataFormats.FileDrop))
{
System.Collections.Specialized.StringCollection sc = Clipboard.GetFileDropList();
for (int i = 0; i < sc.Count; i++)
{
string listfileName = sc[i];
clipboardList.Add(listfileName);
}
}
return clipboardList;
}
/// <summary>
///Get the list of files in the clipboard (method)
/// </summary>
/// <param name="cut">true:剪切;false:复制</param>
/// <returns>System. Collections. List < string > returns the set of file paths in the clipboard < / returns >
public static List<string> GetClipboardList(out bool cut)
{
List<string> clipboardList = new List<string>();
cut = false;
IDataObject dataObject = Clipboard.GetDataObject();
if (dataObject.GetDataPresent(DataFormats.FileDrop))
{
MemoryStream memoryStream = (MemoryStream)dataObject.GetData("Preferred DropEffect", true);
DragDropEffects dragDropEffects = (DragDropEffects)memoryStream.ReadByte();
if ((dragDropEffects & DragDropEffects.Move) == DragDropEffects.Move)
{
cut = true;
}
StringCollection sc = Clipboard.GetFileDropList();
for (int i = 0; i < sc.Count; i++)
{
string listfileName = sc[i];
clipboardList.Add(listfileName);
}
}
return clipboardList;
}