示例#1
0
        public static byte[] getClipboard()
        {
            ClipboardContent cbContent = new ClipboardContent();
            Thread           thread    = new Thread(() =>
            {
                if (Clipboard.ContainsAudio())
                {
                    cbContent = new ClipboardContent(ClipboardContentType.AUDIO, ObjectToByteArray(Clipboard.GetAudioStream()));
                }
                else if (Clipboard.ContainsText())
                {
                    cbContent = new ClipboardContent(ClipboardContentType.TEXT, ObjectToByteArray(Clipboard.GetText()));
                }
                else if (Clipboard.ContainsImage())
                {
                    cbContent = new ClipboardContent(ClipboardContentType.IMAGE, ObjectToByteArray(Clipboard.GetImage()));
                }
                else if (Clipboard.ContainsFileDropList())
                {
                    StringCollection cbEntriesToSend = new StringCollection();
                    foreach (string cbEntry in Clipboard.GetFileDropList())
                    {
                        if (Directory.Exists(cbEntry))
                        {
                            cbEntriesToSend.Add((new DirectoryInfo(cbEntry)).Name);
                        }
                        else if (File.Exists(cbEntry))
                        {
                            cbEntriesToSend.Add(Path.GetFileName(cbEntry));
                        }
                    }
                    cbContent       = new ClipboardContent(ClipboardContentType.FILEDROPLIST, ObjectToByteArray(cbEntriesToSend));
                    cbContent.Files = GetFiles(Clipboard.GetFileDropList());
                }
                else // caso generico
                {
                    cbContent = new ClipboardContent(ClipboardContentType.DATA, ObjectToByteArray(Clipboard.GetDataObject()));
                }
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return(ObjectToByteArray(cbContent));
        }
示例#2
0
        public static void setClipboard(byte[] bytes)
        {
            ClipboardContent cc     = (ClipboardContent)ByteArrayToObject(bytes);
            Thread           thread = new Thread(() =>
            {
                switch (cc.Type)
                {
                case ClipboardContentType.AUDIO:
                    Clipboard.SetAudio(cc.Content);
                    return;

                case ClipboardContentType.TEXT:
                    Clipboard.SetText((String)ByteArrayToObject(cc.Content));
                    return;

                case ClipboardContentType.IMAGE:
                    Clipboard.SetImage((Image)ByteArrayToObject(cc.Content));
                    return;

                case ClipboardContentType.FILEDROPLIST:
                    StringCollection paths = ConvertPaths((StringCollection)ByteArrayToObject(cc.Content));
                    ByteToFile(cc.Files);
                    Clipboard.SetFileDropList(paths);
                    return;

                case ClipboardContentType.DATA:
                    IDataObject dataObject = (IDataObject)ByteArrayToObject(cc.Content);
                    DataObject cbData      = new DataObject();
                    foreach (string format in dataObject.GetFormats())
                    {
                        cbData.SetData(format, false, dataObject.GetData(format, false));
                    }
                    Clipboard.SetDataObject(cbData, true);
                    return;
                }
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }