Winform/WPF Clipboard之剪切复制粘贴

 Winform

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

 wpf

/// <summary>
    /// 复制粘贴帮助类
    /// </summary>
    public class ClipboardHelper
    {

        /// <summary>
        /// 复制到剪切板
        /// </summary>
        /// <param name="text">文字</param> 
        public static void CopyText(string text)
        {
            Clipboard.SetText(text);
        }

        /// <summary>
        /// 复制或剪切文件到剪切板
        /// </summary>
        /// <param name="filePath">文件路径数组</param>
        /// <remarks>清空剪切板</remarks>
        public static void SetFileDrop(string filePath)
        {
            if (filePath == null) return;
            SetFileDropList(new[] { filePath });
        }
        /// <summary>
        /// 复制或剪切文件到剪切板
        /// </summary>
        /// <param name="files">文件路径数组</param>
        /// <remarks>清空剪切板</remarks>
        public static void SetFileDropList(string[] files)
        {
            Clipboard.Clear();//清空剪切板 
            StringCollection strcoll = new StringCollection();
            foreach (var file in files)
            {  
                strcoll.Add(file);
            }
            Clipboard.SetFileDropList(strcoll);
        }

        /// <summary>
        /// 复制或剪切文件到剪切板
        /// </summary>
        /// <param name="filePath">文件路径数组</param>
        /// <param name="cut">true:剪切;false:复制</param>
        public static void CopyFile(string filePath, bool cut = false)
        {
            if (filePath == null) return;
            CopyFileList(new[] { filePath }, cut);
        }
        /// <summary>
        /// 复制或剪切文件到剪切板
        /// </summary>
        /// <param name="files">文件路径数组</param>
        /// <param name="cut">true:剪切;false:复制</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("PreferredDropEffect", memo);
            Clipboard.SetDataObject(data, false);
        }

        /// <summary>
        /// 获取剪贴板中的文件列表(方法)
        /// </summary>
        /// <returns>System.Collections.List<string>返回剪切板中文件路径集合</returns>
        public static List<string> GetClipboardList()
        {
            List<string> clipboardList = new List<string>();
            StringCollection sc = Clipboard.GetFileDropList();
            foreach (var listFileName in sc)
            {
                clipboardList.Add(listFileName);
            }
            return clipboardList;
        }
    }

 

转载于:https://www.cnblogs.com/xcsn/p/4678364.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值