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; } }
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')); }
public void Close() { // Close socket ff exists if (client_socket != null) { SendCommand(FtpCommands.QUIT()); if (client_socket.Connected) { client_socket.Close(); } } client_socket.Dispose(); client_socket = null; // Raise ConnectionClosed event if connected if (ConnectionStatus != FtpConnectionStatus.NotConnected) { ConnectionClosed(this, new EventArgs()); } // Set connection status ConnectionStatus = FtpConnectionStatus.NotConnected; Connected = false; }
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); } }
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); } }
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()); }