コード例 #1
0
        public static FtpFileInfo Parse(string s)
        {
            var parts = s.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            var date = DateTime.ParseExact(parts[0], "mm-dd-yy", CultureInfo.InvariantCulture);
            var time = DateTime.ParseExact(parts[1], "hh:mmtt", CultureInfo.InvariantCulture);
            var fileInfo = new FtpFileInfo
            {
                CreatedTime = date.Date + time.TimeOfDay,
                Size = double.Parse(parts[2]),
                Name = parts[3]
            };

            return fileInfo;
        }
コード例 #2
0
        public async Task GetFileStreamAsync(FtpFileInfo fileInfo, Stream outputStream)
        {
            DownloadStarted?.Invoke(this, new DownloadStartedEventArgs(fileInfo));
            DownloadProgressUpdated?.Invoke(this, new DownloadStatusUpdatedEventArgs(fileInfo, 0));
            var ftpRequest = PrepareRequest(fileInfo.Uri.AbsolutePath, WebRequestMethods.Ftp.DownloadFile);

            try
            {
                using (var response = await ftpRequest.GetResponseAsync())
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream == null)
                    {
                        throw new FtpException();
                    }

                    var streamLength = fileInfo.Size;
                    double streamProcessed = 0;

                    var buffer = new byte[16*1024];
                    int read;
                    while ((read = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                    {
                        await outputStream.WriteAsync(buffer, 0, read);

                        streamProcessed += read;
                        DownloadProgressUpdated?.Invoke(this,
                            new DownloadStatusUpdatedEventArgs(fileInfo, streamProcessed/streamLength*100));
                    }

                    if (fileInfo.Size.Equals(streamProcessed) == false)
                    {
                        throw new FtpException();
                    }
                }
            }
            catch
            {
                DownloadFailed?.Invoke(this, new DownloadFailedEventArgs(fileInfo));
            }

            DownloadCompleted?.Invoke(this, new DownloadCompletedEventArgs(fileInfo));
        }
コード例 #3
0
        private ProcessStatusUiElement CreateStatusUiElements(FtpFileInfo fileInfo)
        {
            var label = new TextBlock
            {
                Text = $"{fileInfo.Name} ({fileInfo.StringSize})",
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(10, 0, 10, 3),
                
            };
            var progressBar = new ProgressBar
            {
                Height = 5,
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(10, 0, 10, 10)
            };

            MainPanel.Children.Add(label);
            Canvas.SetLeft(label, 10);
            Canvas.SetTop(label, 0);
            MainPanel.Children.Add(progressBar);
            Canvas.SetLeft(progressBar, 10);
            Canvas.SetTop(progressBar, 0);

            return new ProcessStatusUiElement
            {
                Label = label,
                ProgressBar = progressBar
            };
        }
コード例 #4
0
 public void GetFileStream(FtpFileInfo fileInfo, Stream outputStream)
 {
     Task.Factory.StartNew(() => GetFileStreamAsync(fileInfo, outputStream).Wait()).Wait();
 }