FtpReply.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. namespace System.Net.FtpClient {
  5. /// <summary>
  6. /// Represents a reply to an event on the server
  7. /// </summary>
  8. public struct FtpReply : IFtpReply {
  9. /// <summary>
  10. /// The type of response received from the last command executed
  11. /// </summary>
  12. public FtpResponseType Type {
  13. get {
  14. int code;
  15. if (Code != null && Code.Length > 0 &&
  16. int.TryParse(Code[0].ToString(), out code)) {
  17. return (FtpResponseType)code;
  18. }
  19. return FtpResponseType.None;
  20. }
  21. }
  22. string m_respCode;
  23. /// <summary>
  24. /// The status code of the response
  25. /// </summary>
  26. public string Code {
  27. get {
  28. return m_respCode;
  29. }
  30. set {
  31. m_respCode = value;
  32. }
  33. }
  34. string m_respMessage;
  35. /// <summary>
  36. /// The message, if any, that the server sent with the response
  37. /// </summary>
  38. public string Message {
  39. get {
  40. return m_respMessage;
  41. }
  42. set {
  43. m_respMessage = value;
  44. }
  45. }
  46. string m_infoMessages;
  47. /// <summary>
  48. /// Informational messages sent from the server
  49. /// </summary>
  50. public string InfoMessages {
  51. get {
  52. return m_infoMessages;
  53. }
  54. set {
  55. m_infoMessages = value;
  56. }
  57. }
  58. /// <summary>
  59. /// General success or failure of the last command executed
  60. /// </summary>
  61. public bool Success {
  62. get {
  63. if (Code != null && Code.Length > 0) {
  64. int i;
  65. // 1xx, 2xx, 3xx indicate success
  66. // 4xx, 5xx are failures
  67. if (int.TryParse(Code[0].ToString(), out i) && i >= 1 && i <= 3) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets the error message including any informational output
  76. /// that was sent by the server. Sometimes the final response
  77. /// line doesn't contain anything informative as to what was going
  78. /// on with the server. Instead it may send information messages so
  79. /// in an effort to give as meaningful as a response as possible
  80. /// the informational messages will be included in the error.
  81. /// </summary>
  82. public string ErrorMessage {
  83. get {
  84. string message = "";
  85. if (Success) {
  86. return message;
  87. }
  88. if (InfoMessages != null && InfoMessages.Length > 0) {
  89. foreach (string s in InfoMessages.Split('\n')) {
  90. string m = Regex.Replace(s, "^[0-9]{3}-", "");
  91. message += string.Format("{0}; ", m.Trim());
  92. }
  93. }
  94. message += Message;
  95. return message;
  96. }
  97. }
  98. }
  99. }