FtpExtensions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Globalization;
  6. namespace System.Net.FtpClient {
  7. /// <summary>
  8. /// Extension methods related to FTP tasks
  9. /// </summary>
  10. public static class FtpExtensions {
  11. /// <summary>
  12. /// Converts the specified path into a valid FTP file system path
  13. /// </summary>
  14. /// <param name="path">The file system path</param>
  15. /// <returns>A path formatted for FTP</returns>
  16. public static string GetFtpPath(this string path) {
  17. if (String.IsNullOrEmpty(path))
  18. return "./";
  19. path = Regex.Replace(path.Replace('\\', '/'), "[/]+", "/").TrimEnd('/');
  20. if (path.Length == 0)
  21. path = "/";
  22. return path;
  23. }
  24. /// <summary>
  25. /// Creates a valid FTP path by appending the specified segments to this string
  26. /// </summary>
  27. /// <param name="path">This string</param>
  28. /// <param name="segments">The path segments to append</param>
  29. /// <returns>A valid FTP path</returns>
  30. public static string GetFtpPath(this string path, params string[] segments) {
  31. if (String.IsNullOrEmpty(path))
  32. path = "./";
  33. foreach (string part in segments) {
  34. if (part != null) {
  35. if (path.Length > 0 && !path.EndsWith("/"))
  36. path += "/";
  37. path += Regex.Replace(part.Replace('\\', '/'), "[/]+", "/").TrimEnd('/');
  38. }
  39. }
  40. path = Regex.Replace(path.Replace('\\', '/'), "[/]+", "/").TrimEnd('/');
  41. if (path.Length == 0)
  42. path = "/";
  43. /*if (!path.StartsWith("/") || !path.StartsWith("./"))
  44. path = "./" + path;*/
  45. return path;
  46. }
  47. /// <summary>
  48. /// Gets the directory name of a path formatted for a FTP server
  49. /// </summary>
  50. /// <param name="path">The path</param>
  51. /// <returns>The parent directory path</returns>
  52. public static string GetFtpDirectoryName(this string path) {
  53. string tpath = (path == null ? "" : path.GetFtpPath());
  54. int lastslash = -1;
  55. if (tpath.Length == 0 || tpath == "/")
  56. return "/";
  57. lastslash = tpath.LastIndexOf('/');
  58. if (lastslash < 0)
  59. return ".";
  60. return tpath.Substring(0, lastslash);
  61. }
  62. /*public static string GetFtpDirectoryName(this string path) {
  63. if (path == null || path.Length == 0 || path.GetFtpPath() == "/")
  64. return "/";
  65. return System.IO.Path.GetDirectoryName(path).GetFtpPath();
  66. }*/
  67. /// <summary>
  68. /// Gets the file name from the path
  69. /// </summary>
  70. /// <param name="path">The full path to the file</param>
  71. /// <returns>The file name</returns>
  72. public static string GetFtpFileName(this string path) {
  73. string tpath = (path == null ? null : path);
  74. int lastslash = -1;
  75. if (tpath == null)
  76. return null;
  77. lastslash = tpath.LastIndexOf('/');
  78. if (lastslash < 0)
  79. return tpath;
  80. lastslash += 1;
  81. if (lastslash >= tpath.Length)
  82. return tpath;
  83. return tpath.Substring(lastslash, tpath.Length - lastslash);
  84. }
  85. /*public static string GetFtpFileName(this string path) {
  86. return System.IO.Path.GetFileName(path).GetFtpPath();
  87. }*/
  88. /// <summary>
  89. /// 尝试将字符串FTP数据表示转换成一个日期时间对象
  90. /// Tries to convert the string FTP date representation into a date time object
  91. /// </summary>
  92. /// <param name="date">The date</param>
  93. /// <param name="style">UTC/Local Time</param>
  94. /// <returns>代表日期的日期时间对象,DateTime.MinValue如果有问题 A date time object representing the date, DateTime.MinValue if there was a problem</returns>
  95. public static DateTime GetFtpDate(this string date, DateTimeStyles style) {
  96. string[] formats = new string[] {
  97. "yyyyMMddHHmmss",
  98. "yyyyMMddHHmmss.fff",
  99. "MMM dd yyyy",
  100. "MMM d yyyy",
  101. "MMM dd HH:mm",
  102. "MMM d HH:mm"
  103. };
  104. DateTime parsed;
  105. if (DateTime.TryParseExact(date, formats, CultureInfo.InvariantCulture, style, out parsed)) {
  106. return parsed;
  107. }
  108. return DateTime.MinValue;
  109. }
  110. }
  111. }