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;
}

WPF

/// <summary>
///Copy and paste help classes
/// </summary>
public static class ClipboardHelper
{

    /// <summary>
    ///Copy to clipboard
    /// </summary>
    /// <param name="text"> 
    public static void CopyText(string text)
    {
        Clipboard.SetText(text);
    }

    /// <summary>
    ///Copy or cut files to the clipboard
    /// </summary>
    ///< param name = "filepath" > file path array < / param >
    ///< remarks > empty the shear plate < / remarks >
    public static void SetFileDrop(string filePath)
    {
        if (filePath == null) 
            return;

        SetFileDropList(new[] { filePath });
    }
    /// <summary>
    ///Copy or cut files to the clipboard
    /// </summary>
    ///< param name = "files" > file path array < / param >
    ///< remarks > empty the shear plate < / remarks >
    public static void SetFileDropList(string[] files)
    {
        Clipboard. Clear();// Empty the shear plate 

        StringCollection strcoll = new StringCollection();

        foreach (var file in files)
        {
            strcoll.Add(file);
        }

        Clipboard.SetFileDropList(strcoll);
    }

    /// <summary>
    ///Copy or cut files to the clipboard
    /// </summary>
    ///< param name = "filepath" > file path array < / param >
    /// <param name="cut">true:Cut;false:Copy</param>
    public static void CopyFile(string filePath, bool cut = false)
    {
        if (filePath == null) 
            return;

        CopyFileList(new[] { filePath }, cut);
    }
    /// <summary>
    ///Copy or cut files to the clipboard
    /// </summary>
    ///< param name = "files" > file path array < / param >
    /// <param name="cut">true:Cut;false:Copy</param>
    public static void CopyFileList(string[] files, bool cut = false)
    {
        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, false);
    }

    /// <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> GetFileList()
    {
        List<string> files = new List<string>();

        IDataObject dataObject = Clipboard.GetDataObject();

        if (dataObject.GetDataPresent(DataFormats.FileDrop))
        {
            StringCollection sc = Clipboard.GetFileDropList();

            foreach (var listFileName in sc)
            {
                files.Add(listFileName);
            }
        }

        return files;
    }

    /// <summary>
    ///Get the list of files in the clipboard (method)
    /// </summary>
    /// <param name="cut">true:Cut;false:Copy</param>
    /// <returns>System. Collections. List < string > returns the set of file paths in the clipboard < / returns >
    public static List<string> GetFileList(out bool cut)
    {
        List<string> files = 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();

            foreach (var listFileName in sc)
            {
                files.Add(listFileName);
            }
        }

        return files;
    }
}
Like The Article To View All

All Comments

Leave a Reply Cancel Reply

Tips: Your email address will not be disclosed!

If you can't see clearly,please click to change...