Exemplo n.º 1
0
        //---<Uploads file through communication Channel>------
        public void upLoadFile(FileTransferMessage msg)
        {
            int totalBytes = 0;

            hrt.Start();
            filename = msg.filename;
            string rfilename = Path.Combine(savePath, filename);

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            using (var outputStream = new FileStream(rfilename, FileMode.Create))
            {
                while (true)
                {
                    int bytesRead = msg.transferStream.Read(block, 0, BlockSize);
                    totalBytes += bytesRead;
                    if (bytesRead > 0)
                    {
                        outputStream.Write(block, 0, bytesRead);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            hrt.Stop();
            Console.Write(
                "\n  Received file \"{0}\" of {1} bytes in {2} microsec.",
                filename, totalBytes, hrt.ElapsedMicroseconds
                );
        }
        void uploadFile(string filename)
        {
            string fqname = Path.Combine(ToSendPath, filename);

            try
            {
                hrt.Start();
                using (var inputStream = new FileStream(fqname, FileMode.Open))
                {
                    FileTransferMessage msg = new FileTransferMessage();
                    msg.filename       = filename;
                    msg.transferStream = inputStream;
                    channel.upLoadFile(msg);
                }
                hrt.Stop();
                Console.Write("\n  Uploaded file \"{0}\" in {1} microsec.", filename, hrt.ElapsedMicroseconds);
            }
            catch
            {
                Console.Write("\n  can't find \"{0}\"", fqname);
            }
        }