Exemplo n.º 1
0
Arquivo: gFTP.cs Projeto: Gpower2/gFTP
        public async Task <FtpFolder> GetFtpFolderDetailsAsync(String argPath, Int32 argLevel)
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = GetFtpWebRequest(argPath);

            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            String contents = "";

            using (FtpWebResponse response = (FtpWebResponse)(await request.GetResponseAsync()))
            {
                Debug.WriteLine($"Directory {argPath} List Complete, status: '{response.StatusDescription}'");

                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    contents = await reader.ReadToEndAsync();

                    Debug.WriteLine(contents);
                }
            }

            FtpFolder f = ParseFtpFolderResponse(argPath, contents);

            // Read subfolders, if argLevel>0
            if (argLevel > 0 && f.Name != "..." && f.Name != ".." && f.Name != ".")
            {
                if (f.Folders.Count > 0)
                {
                    for (int i = 0; i < f.Folders.Count; i++)
                    {
                        DateTime tmpDate = f.Folders[i].Date;
                        f.Folders[i] = await GetFtpFolderDetailsAsync(f.Folders[i].FullPath, argLevel - 1);

                        f.Folders[i].Date = tmpDate;
                    }
                }
            }
            return(f);
        }
Exemplo n.º 2
0
Arquivo: gFTP.cs Projeto: Gpower2/gFTP
        private FtpFolder ParseFtpFolderResponse(String argPath, String argContents)
        {
            FtpFolder fRoot = new FtpFolder();

            fRoot.FullPath = argPath;
            fRoot.Name     = argPath.Replace(FtpDomain, "").Split(new String[] { "/" }, StringSplitOptions.None).Last();
            if (String.IsNullOrWhiteSpace(fRoot.Name))
            {
                fRoot.Name = "/";
            }
            String[] lines = argContents.Replace("\r", "").Split(new String[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (String line in lines)
            {
                String[] elements = GetElements(line);
                if (line.StartsWith("d"))
                {
                    // We have a folder
                    FtpFolder childFolder = new FtpFolder();
                    childFolder.Name     = elements[8];
                    childFolder.FullPath = UrlHelper.Combine(argPath, elements[8]);
                    if (elements[7].Contains(":"))
                    {
                        // the elements[7] contains hour
                        //Debug.WriteLine(String.Format("{0} {1} {2} {3}", elements[5], elements[6].PadLeft(2, '0'), DateTime.Today.Year, elements[7]));
                        childFolder.Date = DateTime.ParseExact(String.Format("{0} {1} {2} {3}", elements[5], elements[6].PadLeft(2, '0'), DateTime.Today.Year, elements[7]),
                                                               "MMM dd yyyy HH:mm",
                                                               System.Globalization.CultureInfo.InvariantCulture
                                                               );
                        if (childFolder.Date >= DateTime.Today.AddDays(1))
                        {
                            childFolder.Date = new DateTime(DateTime.Today.Year - 1, childFolder.Date.Month, childFolder.Date.Day, childFolder.Date.Hour, childFolder.Date.Minute, childFolder.Date.Second);
                        }
                    }
                    else
                    {
                        // the elements[7] contains year
                        //Debug.WriteLine(String.Format("{0} {1} {2}", elements[5], elements[6].PadLeft(2, '0'), elements[7]));
                        childFolder.Date = DateTime.ParseExact(String.Format("{0} {1} {2}", elements[5], elements[6].PadLeft(2, '0'), elements[7]),
                                                               "MMM dd yyyy",
                                                               System.Globalization.CultureInfo.InvariantCulture
                                                               );
                    }
                    fRoot.Folders.Add(childFolder);
                }
                else
                {
                    // We have a file
                    FtpFile childFile = new FtpFile();
                    childFile.Name     = elements[8];
                    childFile.Folder   = fRoot;
                    childFile.FullPath = fRoot.Name;
                    childFile.Size     = Int64.Parse(elements[4]);
                    if (elements[7].Contains(":"))
                    {
                        // the elements[7] contains hour
                        //Debug.WriteLine(String.Format("{0} {1} {2} {3}", elements[5], elements[6].PadLeft(2, '0'), DateTime.Today.Year, elements[7]));
                        childFile.Date = DateTime.ParseExact(String.Format("{0} {1} {2} {3}", elements[5], elements[6].PadLeft(2, '0'), DateTime.Today.Year, elements[7]),
                                                             "MMM dd yyyy HH:mm",
                                                             System.Globalization.CultureInfo.InvariantCulture
                                                             );
                        if (childFile.Date > DateTime.Today.AddDays(1))
                        {
                            childFile.Date = new DateTime(DateTime.Today.Year - 1, childFile.Date.Month, childFile.Date.Day, childFile.Date.Hour, childFile.Date.Minute, childFile.Date.Second);
                        }
                    }
                    else
                    {
                        // the elements[7] contains year
                        //Debug.WriteLine(String.Format("{0} {1} {2}", elements[5], elements[6].PadLeft(2, '0'), elements[7]));
                        childFile.Date = DateTime.ParseExact(String.Format("{0} {1} {2}", elements[5], elements[6].PadLeft(2, '0'), elements[7]),
                                                             "MMM dd yyyy",
                                                             System.Globalization.CultureInfo.InvariantCulture
                                                             );
                    }
                    fRoot.Files.Add(childFile);
                }
            }
            return(fRoot);
        }