Ejemplo n.º 1
0
        public FTPFile GetFTPFile(string ftpPath, string ftpFileName)
        {
            if (_errorMsg != null)
            {
                _errorMsg = null;
            }
            FTPFile retVal = null;

            // fields checking
            if (string.IsNullOrEmpty(ftpPath) || string.IsNullOrEmpty(ftpFileName))
            {
                return(retVal);
            }

            if (!ftpPath.EndsWith("/"))
            {
                ftpPath += "/";
            }
            try
            {
                FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create(ftpPath);
                requestDir.KeepAlive   = this.IsAlive;
                requestDir.UsePassive  = this.IsPassive;
                requestDir.Method      = WebRequestMethods.Ftp.ListDirectoryDetails;
                requestDir.Credentials = new NetworkCredential(this.Login, this.PWD);

                FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse();
                using (StreamReader readerDir = new StreamReader(responseDir.GetResponseStream()))
                {
                    string  line = readerDir.ReadLine();
                    FTPItem item;
                    while (line != null)
                    {
                        item = FTPItem.Parse(line);
                        if ((item != null) && (item.Name == ftpFileName) && (item is FTPFile))
                        {
                            FTPFile ftpFile = (FTPFile)item;
                            retVal = new FTPFile()
                            {
                                ItemType = FSItemTypeEnum.File,
                                FullName = ftpPath + ftpFileName,
                                Name     = ftpFileName,
                                DateTime = ftpFile.DateTime,
                                Size     = ftpFile.Size
                            };
                            break;
                        }
                        line = readerDir.ReadLine();
                    }
                }
                responseDir.Close();
            }
            catch (Exception ex)
            {
                _errorMsg = ex.Message;
            }

            return(retVal);
        }
Ejemplo n.º 2
0
        public FTPFolder GetFTPFolder(string path, bool isRecurse = false)
        {
            if (_errorMsg != null)
            {
                _errorMsg = null;
            }
            FTPFolder retVal = null;

            // fields checking
            if (string.IsNullOrEmpty(path))
            {
                return(retVal);
            }

            try
            {
                if (path.Last() != '/')
                {
                    path += "/";
                }
                retVal = getFTPFolder(path);

                // рекурсивное заполение папок
                if ((retVal != null) && isRecurse)
                {
                    foreach (FTPItem item in retVal.Items)
                    {
                        if (item.ItemType == FSItemTypeEnum.Folder)
                        {
                            FTPItem ftpItem = GetFTPFolder(path + item.Name + "/", true);
                            if (ftpItem != null)
                            {
                                FTPFolder ftpFolder = (FTPFolder)ftpItem;
                                FTPItem[] ftpItems  = new FTPItem[ftpFolder.Items.Count];
                                ftpFolder.Items.CopyTo(ftpItems);
                                ftpFolder = null;

                                ((FTPFolder)item).Items.AddRange(ftpItems);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _errorMsg = ex.Message;
            }

            return(retVal);
        }
Ejemplo n.º 3
0
        public FTPFolder getFTPFolder(string path)
        {
            FTPFolder retVal = null;

            FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create(path);

            FtpWebRequest.DefaultCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            requestDir.KeepAlive             = this.IsAlive;
            requestDir.UsePassive            = this.IsPassive;
            requestDir.Method      = WebRequestMethods.Ftp.ListDirectoryDetails;
            requestDir.Credentials = new NetworkCredential(this.Login, this.PWD);

            FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse();

            using (StreamReader readerDir = new StreamReader(responseDir.GetResponseStream()))
            {
                retVal = new FTPFolder()
                {
                    ItemType = FSItemTypeEnum.Folder, FullName = path
                };

                string  line = readerDir.ReadLine();
                FTPItem item;
                while (line != null)
                {
                    item = FTPItem.Parse(line);
                    if (item != null)
                    {
                        item.FullName = path + item.Name;
                        retVal.Items.Add(item);
                    }
                    line = readerDir.ReadLine();
                }
            }
            responseDir.Close();

            return(retVal);
        }