Esempio n. 1
0
        /// <summary>
        /// Uploads a file from the local machine to the remote machine.
        /// </summary>
        /// <param name="localSource">The fully qualified local file name.</param>
        /// <param name="remoteDestination">The fully qualified remote file name.</param>
        /// <remarks>
        /// <para>
        /// Any existing file on the remote machine will be overwritten by
        /// this method, even if the file is marked as read-only.
        /// </para>
        /// <note>
        /// Any missing parent directories will be created on the remote
        /// machine as necessary.
        /// </note>
        /// </remarks>
        public void UploadFile(string localSource, string remoteDestination)
        {
            lock (syncLock)
            {
                Verify();
                using (var fs = new FileStream(localSource, FileMode.Open, FileAccess.Read))
                {
                    Guid   fileID;
                    byte[] buffer;
                    byte[] data;
                    int    cb;
                    long   pos;

                    fileID = remote.CreateRemoteFile(remoteDestination);
                    buffer = new byte[FileTransferBlockSize];

                    try
                    {
                        while (true)
                        {
                            pos = fs.Position;
                            cb  = fs.Read(buffer, 0, buffer.Length);
                            if (cb == 0)
                            {
                                break;
                            }

                            if (cb == buffer.Length)
                            {
                                data = buffer;
                            }
                            else
                            {
                                data = Helper.Extract(buffer, 0, cb);
                            }

                            remote.RemoteFileWrite(fileID, pos, data);
                        }

                        remote.CloseRemoteFile(fileID);
                    }
                    catch
                    {
                        remote.PurgeRemoteFile(fileID);
                        throw;
                    }
                }
            }
        }