123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Globalization;
- namespace System.Net.FtpClient {
-
-
-
- public static class FtpExtensions {
-
-
-
-
-
- public static string GetFtpPath(this string path) {
- if (String.IsNullOrEmpty(path))
- return "./";
- path = Regex.Replace(path.Replace('\\', '/'), "[/]+", "/").TrimEnd('/');
- if (path.Length == 0)
- path = "/";
- return path;
- }
-
-
-
-
-
-
- public static string GetFtpPath(this string path, params string[] segments) {
- if (String.IsNullOrEmpty(path))
- path = "./";
- foreach (string part in segments) {
- if (part != null) {
- if (path.Length > 0 && !path.EndsWith("/"))
- path += "/";
- path += Regex.Replace(part.Replace('\\', '/'), "[/]+", "/").TrimEnd('/');
- }
- }
- path = Regex.Replace(path.Replace('\\', '/'), "[/]+", "/").TrimEnd('/');
- if (path.Length == 0)
- path = "/";
-
- return path;
- }
-
-
-
-
-
- public static string GetFtpDirectoryName(this string path) {
- string tpath = (path == null ? "" : path.GetFtpPath());
- int lastslash = -1;
- if (tpath.Length == 0 || tpath == "/")
- return "/";
- lastslash = tpath.LastIndexOf('/');
- if (lastslash < 0)
- return ".";
- return tpath.Substring(0, lastslash);
- }
-
-
-
-
-
-
- public static string GetFtpFileName(this string path) {
- string tpath = (path == null ? null : path);
- int lastslash = -1;
- if (tpath == null)
- return null;
- lastslash = tpath.LastIndexOf('/');
- if (lastslash < 0)
- return tpath;
- lastslash += 1;
- if (lastslash >= tpath.Length)
- return tpath;
- return tpath.Substring(lastslash, tpath.Length - lastslash);
- }
-
-
-
-
-
-
-
-
- public static DateTime GetFtpDate(this string date, DateTimeStyles style) {
- string[] formats = new string[] {
- "yyyyMMddHHmmss",
- "yyyyMMddHHmmss.fff",
- "MMM dd yyyy",
- "MMM d yyyy",
- "MMM dd HH:mm",
- "MMM d HH:mm"
- };
- DateTime parsed;
- if (DateTime.TryParseExact(date, formats, CultureInfo.InvariantCulture, style, out parsed)) {
- return parsed;
- }
- return DateTime.MinValue;
- }
- }
- }
|