Exemplo n.º 1
0
        public async Task DownloadFile(Attachment file)
        {
            using (_client = new WebClient())
            {
                var fileName = file.File.FileName;

                if (file.IsEncrypted)
                {
                    fileName = "crypt_download" + Files.Count(f => f.IsNotCompleted);
                }

                _client.DownloadProgressChanged += (o, e) =>
                {
                    file.Progress = e.ProgressPercentage;
                };
                _client.DownloadFileCompleted += (o, e) =>
                {
                    if (file.IsEncrypted)
                    {
                        _cryptTool.DecryptFile(file.Path + "\\" + fileName,
                                               file.Path + "\\" + file.File.FileName,
                                               file.EncryptedSymmetricKey);
                    }
                    file.IsNotCompleted = false;
                };

                try
                {
                    await _client.DownloadFileTaskAsync(file.File.Url, file.Path + "\\" + fileName);
                }
                catch (Exception)
                {
                    // ignored
                }
            }
        }