Пример #1
0
        /// <summary>
        /// Creates a root FTP directory.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="lastModifiedTime"></param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException"><paramref name="uri"/> is null or not represents a FTP URI or is not a absolute URI.</exception>
        public static FtpDirectoryInfo CreateRoot(Uri uri, DateTime?lastModifiedTime)
        {
            if (uri == null)
            {
                throw new ArgumentException("The URI is null.", "uri");
            }
            if (!string.Equals(uri.Scheme, Uri.UriSchemeFtp, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("The URI not represents a FTP URI.", "uri");
            }
            if (!uri.IsAbsoluteUri)
            {
                throw new ArgumentException("The URI not represents a absolute FTP URI.", "uri");
            }

            FtpDirectoryInfo root = new FtpDirectoryInfo {
                IsDirectory    = true,
                Root           = null,
                Parent         = null,
                Name           = Path.GetFileName(uri.LocalPath),
                RelativeUri    = uri.MakeRelativeUri(uri),
                AbsoluteUri    = uri,
                Length         = 0,
                LastModifyTime = (lastModifiedTime ?? DateTime.MinValue),
            };

            root.Root = root;
            return(root);
        }
Пример #2
0
        /// <summary>
        /// Creates a FtpFileInfo object from the detailed information returned from the FTP server.
        /// If the file represents the current directory or parent directory, then return null.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="detail"></param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException"><paramref name="parent"/> is null.</exception>
        /// <exception cref="System.ArgumentException"><paramref name="detail"/> is null or empty.</exception>
        public static FtpFileInfo Create(FtpDirectoryInfo parent, string detail)
        {
            if (parent == null)
            {
                throw new ArgumentException("The parent directory is null.", "parent");
            }
            if (string.IsNullOrWhiteSpace(detail))
            {
                throw new ArgumentException("The detailed information is null or empty.", "detail");
            }

            FtpFileInfo info = null;

            if (Regex.IsMatch(detail, @"^\d"))
            {
                //MS-DOS format
                info = ParseMsDosFormat(detail);
            }
            else
            {
                //Unix format
                info = ParseUnixFormat(detail);
            }

            if (string.Equals(info.Name, CURRENT_DIRECTORY_NAME) || string.Equals(info.Name, PARENT_DIRECTORY_NAME))
            {
                info = null;
            }
            else
            {
                info.Root   = parent.Root;
                info.Parent = parent;
                if (parent.RelativeUri.OriginalString.Length > 0)
                {
                    info.RelativeUri = new Uri(string.Format("{0}/{1}", parent.RelativeUri, info.Name), UriKind.Relative);
                }
                else
                {
                    info.RelativeUri = new Uri(info.Name, UriKind.Relative);
                }
                info.AbsoluteUri = new Uri(parent.AbsoluteUri, info.Name);
                //info.RelativeUri = new Uri(Uri.UnescapeDataString(parent.Root.AbsoluteUri.MakeRelativeUri(info.AbsoluteUri).ToString()), UriKind.Relative);
            }
            return(info);
        }