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

            // Send command
            var reply = SendCommand(FtpCommands.CWD(pathname));

            // Raise exception when wrong reply
            if (reply.ReplyCode != FtpReplyCode.RequestedFileActionCompleted)
            {
                throw new FtpException(reply);
            }
        }
Пример #2
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());
        }