internal void StartTransfer() { FileStream localStream = null; FtpDataStream remoteStream = null; try { // Files just created may still have a file lock, we'll wait a few seconds for read access if needed... if (TransferDirection == TransferDirection.Upload) { FilePath.WaitForReadLock(LocalFileName, m_session.Host.WaitLockTimeout); } m_session.Host.OnBeginFileTransfer(LocalFileName, RemoteFileName, TransferDirection); remoteStream = m_session.ControlChannel.GetDataStream(TransferDirection); m_ftpFileCommandRoutine(RemoteFileName); if (TransferDirection == TransferDirection.Download) { localStream = new FileStream(LocalFileName, FileMode.Create); } else { localStream = new FileStream(LocalFileName, FileMode.Open, FileAccess.Read); } m_streamCopyRoutine(remoteStream, localStream); // Dispose remote stream before testing file transfer result to ensure // we have received the server's response to the file transfer command remoteStream.Dispose(); TestTransferResult(); m_session.Host.OnEndFileTransfer(LocalFileName, RemoteFileName, TransferDirection); } catch { m_session.Host.OnEndFileTransfer(LocalFileName, RemoteFileName, TransferDirection); throw; } finally { // Need to make sure we end data transfer on the session, which would // normally happen automatically when the remote stream is closed if (remoteStream != null) { remoteStream.Dispose(); } else { m_session.EndDataTransfer(); } localStream?.Dispose(); } }
/// <summary> /// Releases the unmanaged resources used by the <see cref="FtpSessionConnected"/> object and optionally releases the managed resources. /// </summary> /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (!m_disposed) { try { // This will be done regardless of whether the object is finalized or disposed. if (disposing) { m_host = null; m_root = null; m_current = null; if ((object)m_ctrlChannel != null) { m_ctrlChannel.Close(); } m_ctrlChannel = null; if ((object)m_dataStream != null) { m_dataStream.Dispose(); } m_dataStream = null; } } finally { m_disposed = true; // Prevent duplicate dispose. } } }
internal void StartTransfer() { FileStream localStream = null; FtpDataStream remoteStream = null; try { // Files just created may still have a file lock, we'll wait a few seconds for read access if needed... if (m_transferDirection == TransferDirection.Upload) { FilePath.WaitForReadLock(m_localFile, m_session.Host.WaitLockTimeout); } m_session.Host.OnBeginFileTransfer(m_localFile, m_remoteFile, m_transferDirection); remoteStream = m_session.ControlChannel.GetDataStream(m_transferDirection); m_ftpFileCommandRoutine(m_remoteFile); if (m_transferDirection == TransferDirection.Download) { localStream = new FileStream(m_localFile, FileMode.Create); } else { localStream = new FileStream(m_localFile, FileMode.Open, FileAccess.Read); } m_streamCopyRoutine(remoteStream, localStream); // Dispose remote stream before testing file transfer result to ensure // we have received the server's response to the file transfer command remoteStream.Dispose(); TestTransferResult(); m_session.Host.OnEndFileTransfer(m_localFile, m_remoteFile, m_transferDirection); } catch { m_session.Host.OnEndFileTransfer(m_localFile, m_remoteFile, m_transferDirection); throw; } finally { remoteStream?.Dispose(); localStream?.Dispose(); } }