/// <summary> /// Obtains a response stream as a string /// </summary> /// <param name="ftp">current FTP request</param> /// <returns>String containing response</returns> /// <remarks>FTP servers typically return strings with CR and /// not CRLF. Use respons.Replace(vbCR, vbCRLF) to convert /// to an MSDOS string</remarks> private string GetStringResponse(FtpWebRequest ftp) { //Get the result, streaming to a string string result = ""; using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse()) { long size = response.ContentLength; using (Stream datastream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(datastream)) { _WelcomeMessage = response.WelcomeMessage; _ExitMessage = response.ExitMessage; result = sr.ReadToEnd(); sr.Close(); } try { //Declare Event NewMessageEventArgs e = new NewMessageEventArgs("RESPONSE", response.StatusDescription, response.StatusCode.ToString(), ""); //Raise Event OnNewMessageReceived(this, e); } catch { } datastream.Close(); } response.Close(); } return(result); }
/// <summary> /// Return a detailed directory listing /// </summary> /// <param name="directory">Directory to list, e.g. /pub/etc</param> /// <returns>An FTPDirectory object</returns> public FTPdirectory ListDirectoryDetail(string directory) { System.Net.FtpWebRequest ftp = GetRequest(GetDirectory(directory)); //Set request to do simple list ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails; //Give Message of Command string str = GetStringResponse(ftp); //replace CRLF to CR, remove last instance str = str.Replace("\r\n", "\r").TrimEnd('\r'); NewMessageEventArgs e = new NewMessageEventArgs("COMMAND", "List Directory Details", "LIST", directory); OnNewMessageReceived(this, e); //split the string into a list return(new FTPdirectory(str, _lastDirectory)); }
/// <summary> /// Determine size of remote file /// </summary> /// <param name="filename"></param> /// <returns></returns> /// <remarks>Throws an exception if file does not exist</remarks> public bool FtpCreateDirectory(string dirpath) { //perform create string URI = this.Hostname + AdjustDir(dirpath); System.Net.FtpWebRequest ftp = GetRequest(URI); //Set request to MkDir ftp.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory; try { string str = GetStringResponse(ftp); //Give Message of Command NewMessageEventArgs e = new NewMessageEventArgs("COMMAND", "Make Directory", "MKD", AdjustDir(dirpath)); OnNewMessageReceived(this, e); } catch (Exception) { return(false); } return(true); }
public bool FtpDeleteDirectory(string dirpath) { //perform remove string URI = this.Hostname + AdjustDir(dirpath); System.Net.FtpWebRequest ftp = GetRequest(URI); //Set request to RmDir ftp.Method = System.Net.WebRequestMethods.Ftp.RemoveDirectory; try { //get response but ignore it string str = GetStringResponse(ftp); //Give Message of Command NewMessageEventArgs e = new NewMessageEventArgs("COMMAND", "Remove Directory", "RMD", dirpath); OnNewMessageReceived(this, e); } catch (Exception) { return(false); } return(true); }
/// <summary> /// Delete remote file /// </summary> /// <param name="filename">filename or full path</param> /// <returns></returns> /// <remarks></remarks> public bool FtpDelete(string filename) { //Determine if file or full path string URI = this.Hostname + GetFullPath(filename); System.Net.FtpWebRequest ftp = GetRequest(URI); //Set request to delete ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; try { //get response but ignore it string str = GetStringResponse(ftp); //Give Message of Command NewMessageEventArgs e = new NewMessageEventArgs("COMMAND", "Delete File", "DELE", str); OnNewMessageReceived(this, e); } catch (Exception) { return(false); } return(true); }