예제 #1
0
        private List <File> GetDirectoryInformation(string address)
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address);

            request.Method      = WebRequestMethods.Ftp.ListDirectory;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive  = true;
            request.UseBinary   = true;
            request.KeepAlive   = false;

            List <File> returnValue = new List <File>();

            string[] list = null;

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    list = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                }

            foreach (string line in list)
            {
                // Windows FTP Server Response Format
                // DateCreated    IsDirectory    Name
                DateTime dateTime = new DateTime();

                // Parse name
                string name = line;

                // Create directory info
                File item = new File();
                item.BaseUri     = new Uri(address);
                item.DateCreated = dateTime;
                item.IsDirectory = name.Contains('.')? false : true;
                item.Name        = name;

                Console.WriteLine(item.AbsolutePath);
                item.Items = item.IsDirectory ? GetDirectoryInformation(item.AbsolutePath) : null;

                returnValue.Add(item);
            }

            return(returnValue);
        }
예제 #2
0
        private void DownloadSingleFile(File file, string destination)
        {
            byte[]        buffer    = new byte[2048];
            int           bytesRead = 0;
            FtpWebRequest request   = CreateFtpWebRequest(file.AbsolutePath, true);

            request.Method = WebRequestMethods.Ftp.DownloadFile;

            Stream     reader     = request.GetResponse().GetResponseStream();
            FileStream fileStream = new FileStream(destination + "/" + file.Name, FileMode.Create);

            double alreadyDownloaded = 0.0f;

            //Get from List of Sizes from method below
            //double fileLength = GetSizeSingleFile(file.AbsolutePath);

            while (true)
            {
                bytesRead = reader.Read(buffer, 0, buffer.Length);

                if (bytesRead == 0)
                {
                    break;
                }

                fileStream.Write(buffer, 0, bytesRead);

                alreadyDownloaded += bytesRead;
                //double percentDone = (alreadyDownloaded / file.Size) * 100.0;
                OnFileDownloadUpdate(new FileDownloadUpdateArgs()
                {
                    CurrentFileSize           = file.Size,
                    CurrentFileDownloadedSize = alreadyDownloaded,
                    FileDownloadedSize        = alreadyDownloaded
                });
            }
            fileStream.Close();
        }
예제 #3
0
        private List <File> GetDirectoryInformationDetails(string address)
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address);

            request.Method      = WebRequestMethods.Ftp.ListDirectoryDetails;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive  = true;
            request.UseBinary   = true;
            request.KeepAlive   = false;

            List <File> returnValue = new List <File>();

            string[] list = null;

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    list = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                }

            foreach (string line in list)
            {
                // Windows FTP Server Response Format
                // DateCreated    IsDirectory    Name
                string   data        = line;
                string   date        = null;
                DateTime dateTime    = new DateTime();
                string   dir         = null;
                bool     isDirectory = false;

                try
                {
                    // Parse date
                    date     = data.Substring(0, 17);
                    dateTime = DateTime.Parse(date);
                    data     = data.Remove(0, 24);

                    // Parse <DIR>
                    dir         = data.Substring(0, 5);
                    isDirectory = dir.Equals("<dir>", StringComparison.InvariantCultureIgnoreCase);
                    data        = data.Remove(0, 5);
                    data        = data.Remove(0, 10);
                }
                catch
                {
                    // Parse <DIR>
                    dir         = data.Substring(0, 5);
                    isDirectory = dir.Equals("<dir>", StringComparison.InvariantCultureIgnoreCase);
                    data        = data.Remove(0, 5);
                    data        = data.Remove(0, 10);

                    // Parse date
                    date     = data.Substring(0, 17);
                    dateTime = DateTime.Parse(date);
                    data     = data.Remove(0, 24);
                }

                // Parse name
                string name = data;

                // Create directory info
                File item = new File();
                item.BaseUri     = new Uri(address);
                item.DateCreated = dateTime;
                item.IsDirectory = isDirectory;
                item.Name        = name;

                Console.WriteLine(item.AbsolutePath);
                item.Items = item.IsDirectory ? GetDirectoryInformation(item.AbsolutePath) : null;

                returnValue.Add(item);
            }

            return(returnValue);
        }