コード例 #1
0
        public static void SendClipboardNotice(IDataObject obj)
        {
            if (obj.GetDataPresent(DataFormats.Text))
            {
                string text = (string)obj.GetData(DataFormats.Text);
                ClipboardTrasfer.SendText(text);
            }
            else if (obj.GetDataPresent(DataFormats.FileDrop))
            {
                /* get the list of absolute file paths actually inside Windows Clipboard */
                var dropList = (string[])obj.GetData(DataFormats.FileDrop, false);

                if (!ConfirmSend(dropList))
                {
                    return;
                }

                /* get parent folder, i.e. the folder in which Windows Clipboard was changed */
                int    lastDirSeparatorIndex = (dropList[0].LastIndexOf('\\') + 1);
                string parentDir             = dropList[0].Remove(lastDirSeparatorIndex);

                string path = "";
                foreach (string s in dropList)
                {
                    path += s.Substring(parentDir.Length);
                    path += "|";
                }
                path = path.Remove(path.Length - 1);

                foreach (string absoluteFilePath in dropList)
                {
                    /*
                     * Check if current absolute file path inside the Clipboard represents
                     * a Directory and (if greater than MAX_SIZE) user confirmed its transfer
                     */
                    if (Directory.Exists(absoluteFilePath))
                    {
                        /* First, send to client the current folder... */
                        ClipboardTrasfer.SendNewFolder(absoluteFilePath, ref parentDir);

                        /* ...and all its subfolders */
                        string[] subDirs = Directory.GetDirectories(absoluteFilePath, "*.*", SearchOption.AllDirectories);
                        foreach (string dir in subDirs)
                        {
                            ClipboardTrasfer.SendNewFolder(dir, ref parentDir);
                        }
                        /* finally, send to client all subfiles in order to 'fill' all previously sent folders */
                        string[] subFiles = Directory.GetFiles(absoluteFilePath, "*.*", System.IO.SearchOption.AllDirectories);
                        foreach (string file in subFiles)
                        {
                            ClipboardTrasfer.SendFile(file, ref parentDir);
                        }
                    }

                    /*
                     * Check if current absolute file path inside the Clipboard represents
                     * a File and (if greater than MAX_SIZE) user confirmed its transfer
                     */
                    else if (File.Exists(absoluteFilePath))
                    {
                        ClipboardTrasfer.SendFile(absoluteFilePath, ref parentDir);
                    }
                }

                /*
                 * Finally, send the path drop list, so that Clipboard could change for the counterpart
                 */
                ClipboardTrasfer.SendPathDropList(path);
            }
            else if (obj.GetDataPresent(DataFormats.Bitmap))
            {
                BitmapSource bitmap = (BitmapSource)obj.GetData(DataFormats.Bitmap);
                ClipboardTrasfer.SendBitmap(bitmap);
            }
        }
コード例 #2
0
 private void StopAllServices(Exception exception)
 {
     ClipboardNetworkChannel.StopService();
     ClipboardTrasfer.StopService();
     InputNetworkChannel.StopService();
 }
コード例 #3
0
        private static void listenClipboard()
        {
            try {
                listener.Listen(0);
                client = listener.Accept();
            } catch (Exception) {
                listener.Close();
                return;
            }

            byte[] rawPacket = new byte[ClipboardConstants.PacketLength];

            while (true)
            {
                try {
                    if (!Utility.ReceiveBytes(client, rawPacket, rawPacket.Length, SocketFlags.None))
                    {
                        break;
                    }

                    ClipboardPacket     packet     = Serialization.FromClipboardBytes(rawPacket);
                    ClipboardPacketType packetType = (ClipboardPacketType)packet.type;

                    switch (packetType)
                    {
                    case ClipboardPacketType.TEXT:
                        Clipboard.SetText(System.Text.Encoding.Unicode.GetString(packet.payload));
                        break;

                    case ClipboardPacketType.DIRECTORY:
                        Clipboard.Clear();
                        string dir = TemporaryDirectory + packet.name;
                        Directory.CreateDirectory(dir);
                        break;

                    case ClipboardPacketType.FILE:
                        string file = TemporaryDirectory + packet.name;
                        lock (objLock) {
                            while (currentDownload != null && !packet.name.Equals(currentFileName))
                            {
                                Monitor.Wait(objLock);
                            }
                            if (currentDownload == null)
                            {
                                currentFileName = packet.name;
                                currentDownload = new AsynchFileReceiver(file, packet.totalLength);
                                Console.WriteLine("START " + file);
                                currentDownload.Start();
                            }
                            currentDownload.newFragmentAvailable(ref packet.payload);
                        }
                        break;

                    case ClipboardPacketType.BITMAP:
                        lock (objLock) {
                            if (bitmapStream == null)
                            {
                                bitmapStream = new MemoryStream((int)packet.totalLength);
                            }
                            bitmapStream.Write(packet.payload, 0, packet.payload.Length);
                            if (bitmapStream.Position == packet.totalLength)
                            {
                                var bitmap = new BitmapImage();
                                bitmap.BeginInit();
                                bitmap.StreamSource = bitmapStream;
                                bitmap.CacheOption  = BitmapCacheOption.OnLoad;
                                bitmap.EndInit();
                                bitmap.Freeze();
                                Clipboard.SetImage(bitmap);
                                bitmapStream.Dispose();
                                bitmapStream = null;
                            }
                        }
                        break;

                    case ClipboardPacketType.UPDATE:
                        ClipboardTrasfer.SendClipboardNotice(Clipboard.GetDataObject());
                        break;

                    case ClipboardPacketType.SET_DROPLIST:
                        string           fileNames  = Encoding.Unicode.GetString(packet.payload);
                        string[]         filesArray = fileNames.Split('|');
                        StringCollection sc         = new StringCollection();
                        foreach (string s in filesArray)
                        {
                            sc.Add(TemporaryDirectory + s);
                        }
                        try {
                            Thread t = new Thread(new ThreadStart(() => {
                                Clipboard.SetFileDropList(sc);
                            }));
                            t.SetApartmentState(ApartmentState.STA);
                            t.Start();
                        } catch (Exception) { }
                        break;
                    }
                } catch (SocketException se) {
                    if (NetworkErrorOccured != null)
                    {
                        NetworkErrorOccured(se);
                    }
                    //break;
                } catch (ObjectDisposedException) {
                    break;
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }
            client.Close();
            listener.Close();
        }