using System; using System.IO; using System.Text.RegularExpressions; namespace FluentFTP { /// /// Represents a reply to an event on the server /// public struct FtpReply { /// /// The type of response received from the last command executed /// public FtpResponseType Type { get { int code; if (Code != null && Code.Length > 0 && int.TryParse(Code[0].ToString(), out code)) { return (FtpResponseType)code; } return FtpResponseType.None; } } string m_respCode; /// /// The status code of the response /// public string Code { get { return m_respCode; } set { m_respCode = value; } } string m_respMessage; /// /// The message, if any, that the server sent with the response /// public string Message { get { return m_respMessage; } set { m_respMessage = value; } } string m_infoMessages; /// /// Informational messages sent from the server /// public string InfoMessages { get { return m_infoMessages; } set { m_infoMessages = value; } } /// /// General success or failure of the last command executed /// public bool Success { get { if (Code != null && Code.Length > 0) { int i; // 1xx, 2xx, 3xx indicate success // 4xx, 5xx are failures if (int.TryParse(Code[0].ToString(), out i) && i >= 1 && i <= 3) { return true; } } return false; } } /// /// Gets the error message including any informational output /// that was sent by the server. Sometimes the final response /// line doesn't contain anything informative as to what was going /// on with the server. Instead it may send information messages so /// in an effort to give as meaningful as a response as possible /// the informational messages will be included in the error. /// public string ErrorMessage { get { string message = ""; if (Success) { return message; } if (InfoMessages != null && InfoMessages.Length > 0) { foreach (string s in InfoMessages.Split('\n')) { string m = Regex.Replace(s, "^[0-9]{3}-", ""); message += string.Format("{0}; ", m.Trim()); } } message += Message; return message; } } } }