Exemplo n.º 1
0
        /// <summary>
        /// This downloads a specified File and passes any Progress to the provided ProgressDOs(if any)
        /// </summary>
        /// <param name="remoteFile">Remote file.</param>
        /// <param name="localFile">Local file.</param>
        /// <param name="progress">Progress.</param>
        /// <param name="total">Total-Progress.</param>
        public void DownloadFile(string remoteFile, string localFile, ProgressDO progress = null, ProgressDO total = null)
        {
            try
            {
                long totalsize = 0;
                if (progress != null)
                {
                    totalsize = GetFileSize(remoteFile);
                    progress.TotlalProgress = totalsize;
                }
                /* Create an FTP Request */
                CreateRequest(WebRequestMethods.Ftp.DownloadFile, remoteFile);
                /* Establish Return Communication with the FTP Server */
                using (_ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse())
                {
                    /* Get the FTP Server's Response Stream */
                    using (_ftpStream = _ftpResponse.GetResponseStream())
                    {
                        /* Open a File Stream to Write the Downloaded File */
                        using (FileStream localFileStream = new FileStream(localFile, FileMode.Create))
                        {
                            /* Buffer for the Downloaded Data */
                            byte[] byteBuffer = new byte[_bufferSize];
                            int    bytesRead  = _ftpStream.Read(byteBuffer, 0, _bufferSize);
                            /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
                            try
                            {
                                while (bytesRead > 0)
                                {
                                    localFileStream.Write(byteBuffer, 0, bytesRead);
                                    if (progress != null)
                                    {
                                        progress.Progress += bytesRead;
                                    }
                                    if (total != null)
                                    {
                                        total.Progress += bytesRead;
                                    }

                                    bytesRead = _ftpStream.Read(byteBuffer, 0, _bufferSize);
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                            /* Resource Cleanup */
                        }
                    }
                }
                _ftpRequest = null;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return;
        }
        public FTPUpdaterModel()
        {
            _loadThread = new ThreadWrap(DoUpdate, "FTPUpdater.LoadThread", false);
            _loadThread.ThreadExceptionOccurred += _loadThread_ThreadExceptionOccurred;
            LogEntries = new ObservableCollection <LogEntryDO>();

            FileProgress   = new ProgressDO();
            GlobalProgress = new ProgressDO();

            DoUpdateCommand = new RelayCommand(x => StartUpdate(), x => CanUpdate());
            DoBrowseCommand = new RelayCommand(x => BrowseTargetFolder());
            _settings       = Settings.Instance;
            _ftp            = new FTPConnection(_settings.FTPServerAdress, _settings.FTPUser, _settings.FTPPass);
        }