Exemple #1
0
        public List <FtpItem> GetFtpItemsByAbsolutelyUrl(string preurl)
        {
            var reg2       = new Regex("\\s\\d+?\\s");
            var list       = new List <FtpItem>();
            var ftpRequest = (FtpWebRequest)WebRequest.Create(preurl);

            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            using (var reader = new StreamReader(ftpRequest.GetResponse().GetResponseStream())) {
                string str;
                while ((str = reader.ReadLine()) != null)
                {
                    var item = new FtpItem {
                        ReviseTimestamp = str.Substring(0, 16),
                    };
                    if (str.IndexOf(DirTag) == -1)
                    {
                        item.FtpItemType = FtpItemType.File;
                        var sizestr = reg2.Match(str).Groups[0].Value;
                        item.OriSize     = long.Parse(sizestr);
                        item.Name        = str.Substring(str.IndexOf(sizestr) + sizestr.Length);
                        item.AbsoluteUri = new Uri(preurl + item.Name);
                    }
                    else
                    {
                        item.FtpItemType = FtpItemType.Folder;
                        item.Name        = str.Substring(str.LastIndexOf(DirTag) + 5).TrimStart();
                        item.AbsoluteUri = new Uri(preurl + item.Name + "/");
                    }
                    list.Add(item);
                }
            }
            return(list);
        }
Exemple #2
0
 public List <FtpItem> GetFtpItems(FtpItem ftpItem)
 {
     if (ftpItem.FtpItemType == FtpItemType.Folder)
     {
         return(GetFtpItemsByAbsolutelyUrl(ftpItem.AbsoluteUri.ToString()));
     }
     throw new Exception("该方法只用于目录项");
 }
Exemple #3
0
 private void GetAllChilds(FtpItem fpItem)
 {
     foreach (var item in fpItem.FolderItems)
     {
         if (item.FtpItemType == FtpItemType.Folder)
         {
             item.FolderItems = GetFtpItems(item);
             GetAllChilds(item);
         }
     }
 }
Exemple #4
0
        public static bool Exist(FtpItem item)
        {
            switch (item.FtpItemType)
            {
            case FtpItemType.File:
                return(FileExist(item.AbsoluteUri.ToString()));

            case FtpItemType.Folder:
                return(FolderExist(item.AbsoluteUri.ToString()));
            }
            throw new ArgumentException("未定义的枚举");
        }
Exemple #5
0
        /// <summary>
        /// 根据baseurl生成一条ftp链
        /// </summary>
        public FtpItem GetFtpLink()
        {
            var link = new FtpItem {
                //Name = "",
                AbsoluteUri = new Uri(BaseUrl),
                FtpItemType = FtpItemType.Folder,
            };

            link.FolderItems = GetFtpItems(link);
            GetAllChilds(link);
            return(link);
        }
Exemple #6
0
        public void DeleteItem(FtpItem item)
        {
            switch (item.FtpItemType)
            {
            case FtpItemType.File:
                DeleteAbsoluteFile(item.AbsoluteUri.ToString());
                break;

            case FtpItemType.Folder:
                DeleteAbsoluteDirectory(item.AbsoluteUri.ToString());
                break;

            default:
                throw new ArgumentException("未定义的枚举值");
            }
        }