Пример #1
0
        //public bool exists(string remotefilename)
        //{
        //    return true;
        //}

        /// <summary>
        /// Fetch all files/folders in this directory and return the ftpinfo array.
        /// </summary>
        public List<Ftpinfo> Browse(string path) //eg: "ftp.xyz.org", "ftp.xyz.org/ftproot/etc"
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            List<Ftpinfo> files = new List<Ftpinfo>();

            //request.Proxy = System.Net.WebProxy.GetDefaultProxy();
            //request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
            request.Credentials = new NetworkCredential(_username, _password);
            Stream rs = request.GetResponse().GetResponseStream();

            OnStatusChange("CONNECTED: " + path, 0, 0);

            if (rs != null)
            {
                StreamReader sr = new StreamReader(rs);
                string strList = sr.ReadToEnd();
                string[] lines = null;

                if (strList.Contains("\r\n"))
                {
                    lines = strList.Split(new[] { "\r\n" }, StringSplitOptions.None);
                }
                else if (strList.Contains("\n"))
                {
                    lines = strList.Split(new[] { "\n" }, StringSplitOptions.None);
                }

                //now decode this string array

                if (lines == null || lines.Length == 0)
                    return null;

                foreach (string line in lines)
                {
                    if (line.Length == 0)
                        continue;
                    //parse line
                    Match m = GetMatchingRegex(line);
                    if (m == null)
                    {
                        //failed
                        throw new ApplicationException("Unable to parse line: " + line);
                    }

                    Ftpinfo item = new Ftpinfo();
                    item.Filename = m.Groups["name"].Value.Trim('\r');
                    item.Path = path;
                    if (m.Groups["size"] != null && !string.IsNullOrWhiteSpace(m.Groups["size"].Value))
                        item.Size = Convert.ToInt64(m.Groups["size"].Value);
                    if ((m.Groups["permission"] != null))
                        item.Permission = m.Groups["permission"].Value;

                    string dir = m.Groups["dir"].Value;
                    if (dir.Length > 0 && dir != "-")
                    {
                        item.FileType = DirectoryEntryTypes.Directory;
                    }
                    else
                    {
                        item.FileType = DirectoryEntryTypes.File;
                    }

                    try
                    {
                        item.FileDateTime = DateTime.Parse(m.Groups["timestamp"].Value);
                    }
                    catch
                    {
                        item.FileDateTime = DateTime.MinValue; //null;
                    }

                    files.Add(item);
                }
            }

            return files;
        }
Пример #2
0
        private static void GetFtpFiles(Ftpinfo ftpinfo, Ftp ftp, string siteurl)
        {
          
            if (ftpinfo.FileType == DirectoryEntryTypes.Directory)
            {
                var subFiles = ftp.Browse(siteurl + ftpinfo.Filename);
                if (subFiles == null)
                {
                    return;
                }
                foreach (var subFile in subFiles)
                {
                   
                   // Console.WriteLine($"File Name: {subFile.Filename}, File Type: {subFile.FileType}");
                    if (subFile.FileType == DirectoryEntryTypes.Directory)
                    {
                        var path = subFile.Path;
                        if (!path.EndsWith("/"))
                        {
                            path = path + "/";
                        }
                        GetFtpFiles(subFile, ftp, path );
                    }
                    else
                    {
                        var fileTime = subFile.FileDateTime;
                        var kftpfolder = ConfigurationManager.AppSettings["ftpSaveDestination"]; 
                        var fileName = subFile.Filename;
                        var fileDate = fileTime.Date;
                        var arr = subFile.Path.Split('/');
                        var folder = arr[arr.Length-1];
                        var folderFullPath = Path.Combine(kftpfolder, folder, fileDate.ToString("MMddyyyy"));
                        if (!Directory.Exists(folderFullPath))
                        {
                            Directory.CreateDirectory(folderFullPath);
                        }
                        var ftpFileName = Path.Combine(folderFullPath, fileName);
                      //  Console.WriteLine($"Name: {subFile.Filename}\tSize: {subFile.Size}");
                        if (fileTime < new DateTime(2016,1,16) || File.Exists(ftpFileName))
                        {
                           // Console.WriteLine($"{ftpFileName} already exists");
                        }
                        else
                        {
                            Console.WriteLine($"New file found: {fileName}");
                            var path = subFile.Path;
                            if (!path.EndsWith("/"))
                            {
                                path = path + "/";
                            }
                            FtpDownload.Fileinfo fileInfo = new FtpDownload.Fileinfo(path + fileName,ftpFileName,DirectionEnum.Down,true);
                           // ftp.AddFileToDownloadQueue(path + fileName, ftpFileName);
                           // ftp.StartProcessing();
                            ftp.Download(fileInfo);
                            var lineCount = File.ReadLines(ftpFileName).Count();
                            // minus 2 for Header line and blank line after header line

                            FileDetail fileDetail = new FileDetail
                            {
                                FileName = fileName,
                                DownloadTime = DateTime.Now,
                                FileDate = fileTime,
                                Folder = folder,
                                Records = lineCount - 2
                            };
                            _list.Add(fileDetail);
                            //_dbHelper.InsertFtpRecord(fileDetail);
                        }
                    }
                }
            }
        }