示例#1
0
        private IEnumerable <WebFileSystemInfo> GetFileSystemInfos(Uri directory, WebFileSystemInfoType type)
        {
            List <WebFileSystemInfo> infos = new List <WebFileSystemInfo>();
            Regex regex = new Regex(@"^(\d+-\d+-\d+\s+\d+:\d+(?:AM|PM))\s+(<DIR>|\d+)\s+(.+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
            Match match;

            using (FtpWebResponse response = (FtpWebResponse)(CreateRequest(directory, WebRequestMethods.Ftp.ListDirectoryDetails).GetResponse())) {
                using (Stream stream = response.GetResponseStream()) {
                    using (StreamReader reader = new StreamReader(stream)) {
                        while (!reader.EndOfStream)
                        {
                            match = regex.Match(reader.ReadLine());

                            if (type == WebFileSystemInfoType.Directory && match.Groups[2].Value == "<DIR>")
                            {
                                infos.Add(new WebFileSystemInfo(new Uri(directory, match.Groups[3].Value), type));
                            }
                            else if (type == WebFileSystemInfoType.File && match.Groups[2].Value != "<DIR>")
                            {
                                infos.Add(new WebFileSystemInfo(new Uri(directory, match.Groups[3].Value), type, DateTime.ParseExact(match.Groups[1].Value, "MM-dd-yy  hh:mmtt", DateTimeFormatInfo.InvariantInfo), long.Parse(match.Groups[2].Value)));
                            }
                        }
                    }
                }
            }

            return(infos);
        }
示例#2
0
 public WebFileSystemInfo(Uri uri, WebFileSystemInfoType type, DateTime lastWriteTime = default(DateTime), long length = 0)
 {
     Uri           = uri;
     Type          = type;
     LastWriteTime = lastWriteTime;
     Length        = length;
 }
示例#3
0
        private IEnumerable <WebFileSystemInfo> GetFileSystemInfos(Uri directory, WebFileSystemInfoType type)
        {
            List <WebFileSystemInfo> infos = new List <WebFileSystemInfo>();

            byte[] pfbs = UTF8Encoding.Default.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?><propfind xmlns=\"DAV:\"><prop><getlastmodified/><displayname/><getcontentlength/><iscollection/></prop></propfind>");

            HttpWebRequest request = CreateRequest(directory, WebDAVRequestMethods.PropFind, new Dictionary <string, string> {
                { "Depth", "1" }
            });

            request.ContentLength = pfbs.Length;
            request.ContentType   = "text/plain;charset=utf-8";

            using (Stream requestStream = request.GetRequestStream()) {
                requestStream.Write(pfbs, 0, pfbs.Length);
            }
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
                using (Stream responseStream = response.GetResponseStream()) {
                    XmlDocument         doc = new XmlDocument();
                    XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
                    xnm.AddNamespace("D", "DAV:");

                    doc.Load(responseStream);

                    foreach (XmlNode item in doc.SelectNodes("/D:multistatus/D:response", xnm))
                    {
                        XmlNode node  = item.SelectSingleNode("D:href", xnm);
                        XmlNode node2 = item.SelectSingleNode("D:propstat/D:prop/D:iscollection", xnm);
                        XmlNode node3 = item.SelectSingleNode("D:propstat/D:prop/D:displayname", xnm);
                        XmlNode node4 = item.SelectSingleNode("D:propstat/D:prop/D:getlastmodified", xnm);
                        XmlNode node5 = item.SelectSingleNode("D:propstat/D:prop/D:getcontentlength", xnm);
                        if (type == WebFileSystemInfoType.Directory && node2.InnerText == "1")
                        {
                            if (node3.InnerText == "/")
                            {
                                continue;
                            }
                            infos.Add(new WebFileSystemInfo(new Uri(node.InnerText), type));
                        }
                        else if (type == WebFileSystemInfoType.File && node2.InnerText == "0")
                        {
                            infos.Add(new WebFileSystemInfo(new Uri(node.InnerText), type, DateTime.Parse(node4.InnerText), long.Parse(node5.InnerText)));
                        }
                    }
                }
            }

            return(infos);
        }