Пример #1
0
        public void RenameFile(string oldFilename, string newFilename)
        {
            // Check opened connection
            if (!Connected)
            {
                throw new FtpNotConnectedException();
            }

            // Send command rename from
            FtpReply reply = SendCommand(FtpCommands.RNFR(oldFilename));

            // Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.RequestedFileActionPendingFurtherInformation)
            {
                throw new FtpException(reply);
            }

            // Send command rename to
            reply = SendCommand(FtpCommands.RNTO(newFilename));
            //Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.RequestedFileActionCompleted)
            {
                throw new FtpException(reply);
            }
        }
Пример #2
0
        private FtpReply SendCommand(string command, bool setStatus = true)
        {
            try
            {
                if (setStatus)
                {
                    ConnectionStatus = FtpConnectionStatus.Busy;
                }
                SendData(command);
                FtpReply reply = ReadReply();
                if (setStatus)
                {
                    ConnectionStatus = FtpConnectionStatus.Ready;
                }
                return(reply);
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == 10035 || ex.ErrorCode == 10050 || ex.ErrorCode == 10051 || ex.ErrorCode == 10052 || ex.ErrorCode == 10053 || ex.ErrorCode == 10060 || ex.ErrorCode == 10064)
                {
                    ConnectionStatus = FtpConnectionStatus.NotConnected;
                    client_socket    = null;
                    Close();
                }
                else
                {
                    ConnectionStatus = FtpConnectionStatus.Ready;
                }

                throw;
            }
        }
Пример #3
0
        public string[] GetFileList(string pathname)
        {
            // Check opened connection
            if (!Connected)
            {
                throw new FtpNotConnectedException();
            }

            // Set connection status to Busy
            this.ConnectionStatus = FtpConnectionStatus.Busy;

            // Create data socket
            Socket socket = CreateDataSocket();

            // Send command
            FtpReply reply = SendCommand(FtpCommands.NLST(pathname), false);

            // Raise exception if wrong reply
            if (reply.ReplyCode != FtpReplyCode.DataConnectionAlreadyOpenTransferStarting && reply.ReplyCode != FtpReplyCode.FileStatusOk)
            {
                this.ConnectionStatus = FtpConnectionStatus.Ready;
                throw new FtpException(reply);
            }

            // Set connection status to Transfering
            this.ConnectionStatus = FtpConnectionStatus.Transfering;

            // Read data
            string text = String.Empty;
            int    num;

            do
            {
                num   = socket.Receive(this.buffer, this.buffer.Length, SocketFlags.None);
                text += Encoding.ASCII.GetString(this.buffer, 0, num);
            }while (num > 0);

            // Close data socket
            socket.Close();

            // Set connection status back to Busy
            this.ConnectionStatus = FtpConnectionStatus.Busy;

            // Read end data transfer reply
            reply = ReadReply();

            // Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.ClosingDataConnection && reply.ReplyCode != FtpReplyCode.RequestedFileActionCompleted)
            {
                this.ConnectionStatus = FtpConnectionStatus.Ready;
                throw new FtpException(reply);
            }

            // Set connection status to Ready
            this.ConnectionStatus = FtpConnectionStatus.Ready;

            // Return list of files
            return(text.Split('\n'));
        }
Пример #4
0
        public void DeleteFile(string filename)
        {
            // Check opened connection
            if (!Connected)
            {
                throw new FtpNotConnectedException();
            }

            // Send command
            FtpReply reply = SendCommand(FtpCommands.DELE(filename));

            //Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.RequestedFileActionCompleted)
            {
                throw new FtpException(reply);
            }
        }
Пример #5
0
        public void DeleteDirectory(string pathname)
        {
            // Check opened connection
            if (!Connected)
            {
                throw new FtpNotConnectedException();
            }

            // Send command
            FtpReply reply = SendCommand(FtpCommands.RMD(pathname));

            // Raise exception if wrong response
            if (reply.ReplyCode != FtpReplyCode.PathCreated && reply.ReplyCode != FtpReplyCode.RequestedFileActionCompleted)
            {
                throw new FtpException(reply);
            }
        }
Пример #6
0
        private void SetBinaryMode(bool isBinary)
        {
            FtpReply reply = null;

            if (isBinary)
            {
                reply = SendCommand(FtpCommands.TYPE("I"));
            }
            else
            {
                reply = SendCommand(FtpCommands.TYPE("A"));
            }

            if (reply.ReplyCode != FtpReplyCode.OK)
            {
                throw new FtpException(reply);
            }
        }
Пример #7
0
        public string GetWorkingDirectory()
        {
            // Check opened connection
            if (!Connected)
            {
                throw new FtpNotConnectedException();
            }

            // Send command
            FtpReply reply = SendCommand(FtpCommands.PWD());

            // Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.PathCreated)
            {
                throw new FtpException(reply);
            }

            // Return directory name
            int q1 = reply.Message.IndexOf('"');
            int q2 = reply.Message.Substring(q1 + 1).IndexOf('"');

            return(reply.Message.Substring(q1 + 1, q2));
        }
Пример #8
0
        public void DownloadFile(string remoteFilename, string localFilename, bool createDirectoryIfNotExists)
        {
            // Check opened connection
            if (!Connected)
            {
                throw new FtpNotConnectedException();
            }

            // Set connection status
            ConnectionStatus = FtpConnectionStatus.Busy;

            // Create local directory if not exists
            FileInfo fileInfo = new FileInfo(localFilename);

            if (createDirectoryIfNotExists)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(fileInfo.DirectoryName);
                if (!directoryInfo.Exists)
                {
                    directoryInfo.Create();
                }
            }

            // Set binary mode
            SetBinaryMode(true);

            // Open data socket
            Socket socket = this.CreateDataSocket();
            // Open file
            FileStream fileStream = new FileStream(localFilename, FileMode.OpenOrCreate);

            // Send command retrieve
            FtpReply reply = SendCommand(FtpCommands.RETR(remoteFilename));

            // Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.FileStatusOk && reply.ReplyCode != FtpReplyCode.DataConnectionAlreadyOpenTransferStarting && reply.ReplyCode != FtpReplyCode.RestartMarkerReply)
            {
                throw new FtpException(reply);
            }

            // Read data from data socket
            int num;

            do
            {
                num = socket.Receive(this.buffer, this.buffer.Length, SocketFlags.None);
                fileStream.Write(this.buffer, 0, num);
            }while (num > 0);

            // Close file
            fileStream.Close();
            // Close data socket
            if (socket.Connected)
            {
                socket.Close();
            }

            // Read end of transfer message
            reply = ReadReply();
            // Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.ClosingDataConnection && reply.ReplyCode != FtpReplyCode.RequestedFileActionCompleted)
            {
                throw new FtpException(reply);
            }
        }
Пример #9
0
        public void UploadFile(string localFilename, string remoteFilename, bool createDirectoryIfNotExists)
        {
            // Check opened connection
            if (!Connected)
            {
                throw new FtpNotConnectedException();
            }

            // Set connection status
            this.ConnectionStatus = FtpConnectionStatus.Busy;

            // Create directory if not exists
            if (createDirectoryIfNotExists)
            {
                if (remoteFilename.LastIndexOf('/') != -1)
                {
                    int    length   = remoteFilename.LastIndexOf('/');
                    string pathname = remoteFilename.Substring(0, length);
                    if (!DirectoryExists(pathname))
                    {
                        CreateDirectory(pathname);
                    }
                }
                if (remoteFilename.LastIndexOf('\\') != -1)
                {
                    int    length    = remoteFilename.LastIndexOf('\\');
                    string pathname2 = remoteFilename.Substring(0, length);
                    if (!DirectoryExists(pathname2))
                    {
                        CreateDirectory(pathname2);
                    }
                }
            }

            // Set binary mode
            SetBinaryMode(true);

            // Open file
            FileStream fileStream = new FileStream(localFilename, FileMode.Open);
            // Open data socket
            Socket socket = this.CreateDataSocket();

            // Send command store
            FtpReply reply = SendCommand(FtpCommands.STOR(remoteFilename));

            // Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.FileStatusOk && reply.ReplyCode != FtpReplyCode.DataConnectionAlreadyOpenTransferStarting)
            {
                throw new FtpException(reply);
            }

            // Send data
            int size;

            while ((size = fileStream.Read(this.buffer, 0, this.buffer.Length)) > 0)
            {
                socket.Send(this.buffer, size, SocketFlags.None);
            }

            // Close file
            fileStream.Close();
            // Close data socket
            if (socket.Connected)
            {
                socket.Close();
            }

            // Read end of transfer message
            reply = ReadReply();
            // Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.ClosingDataConnection && reply.ReplyCode != FtpReplyCode.RequestedFileActionCompleted)
            {
                throw new FtpException(reply);
            }
        }
Пример #10
0
        public void Open()
        {
            ConnectionStatus = FtpConnectionStatus.Connecting;
            client_socket    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint remoteEP;

            try
            {
                remoteEP = new IPEndPoint(Dns.GetHostEntry(RemoteHost).AddressList[0], remotePort);
            }
            catch (SocketException ex)
            {
                ConnectionStatus = FtpConnectionStatus.NotConnected;
                Connected        = false;
                throw new Exception("Can't translate domain name to IP adress", ex);
            }
            try
            {
                this.client_socket.Connect(remoteEP);
            }
            catch (Exception ex)
            {
                ConnectionStatus = FtpConnectionStatus.NotConnected;
                Connected        = false;
                throw new Exception("Can't connect to remote server", ex);
            }
            FtpReply reply = null;

            try
            {
                reply = ReadReply();
            }
            catch (Exception ex)
            {
                Close();
                throw new FtpException(FtpReplyCode.OK, reply.Reply, ex);
            }
            if (reply.ReplyCode != FtpReplyCode.ServiceReadyForNewUser)
            {
                Close();
                throw new FtpException(reply);
            }
            ConnectionStatus = FtpConnectionStatus.LogingIn;
            reply            = SendCommand(FtpCommands.USER(this.remoteUserName));
            if (reply.ReplyCode != FtpReplyCode.NeedPassword && reply.ReplyCode != FtpReplyCode.UserLoggedIn)
            {
                Close();
                throw new FtpException(reply);
            }
            if (reply.ReplyCode != FtpReplyCode.UserLoggedIn)
            {
                reply = SendCommand(FtpCommands.PASS(this.remotePassword));
                if (reply.ReplyCode != FtpReplyCode.UserLoggedIn && reply.ReplyCode != FtpReplyCode.CommandNotImplementedSuperfluousAtThisSite)
                {
                    Close();
                    throw new FtpException(reply);
                }
            }
            Connected        = true;
            ConnectionStatus = FtpConnectionStatus.Busy;
            SendCommand(FtpCommands.CWD(this.remotePath));
            ConnectionStatus = FtpConnectionStatus.Ready;
            ConnectionOpened(this, new EventArgs());
        }