0

私は xamarin.forms アプリケーションに取り組んでいます。HttpClient を使用して複数のファイルを同時にダウンロードしたい。複数のファイルがある場合、System.IO.IOException:Sharing violation on path を取得しています。改善しなければならないことはありますか?ファイルをダウンロードするための私のコードは次のとおりです。

    public async Task DownloadFileAsync(string sourceUrl, string filePathWhereToSave, CancellationTokenSource cts)
    {
        Exception error = null;
        bool isCancelled = false;            
        try
        {
            if (!downloadingTasks.ContainsKey(sourceUrl))
                downloadingTasks.Add(sourceUrl, cts);

            var token = cts.Token;
            var response = await _client.GetAsync(sourceUrl, HttpCompletionOption.ResponseHeadersRead, token);
            response.EnsureSuccessStatusCode();

            string fileName = filePathWhereToSave.Substring(filePathWhereToSave.LastIndexOf('/'));
            string directory = filePathWhereToSave.Substring(0, filePathWhereToSave.LastIndexOf('/'));
            if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            var totalData = response.Content.Headers.ContentLength.GetValueOrDefault(-1L);
            var canSendProgress = totalData != -1L;

            await Task.Run(async() =>
            {
                using (var fileStream = OpenStream(filePathWhereToSave))
                {
                    using (var stream = await response.Content.ReadAsStreamAsync())
                    {
                        var totalRead = 0L;
                        var buffer = new byte[bufferSize];
                        var isMoreDataToRead = true;

                        do
                        {
                            var read = await stream.ReadAsync(buffer, 0, buffer.Length, token);

                            if (read == 0)
                                isMoreDataToRead = false;
                            else
                            {
                                await fileStream.WriteAsync(buffer, 0, read);

                                totalRead += read;

                                if (canSendProgress)
                                {
                                    //var progress = ((totalRead * 1d) / (totalData * 1d) * 100);
                                    MessagingCenter.Send<DownloadFileProgressChangedMessage>(new DownloadFileProgressChangedMessage(sourceUrl, totalRead, totalData, 0), MessageNameConstants.DownloadFileProgressChangedMessage);
                                }
                            }
                        } while (isMoreDataToRead);
                    }
                }
            });
        }
        catch (OperationCanceledException ex)
        {
            isCancelled = true;
        }
        catch (Exception e)
        {
            error = e;
            System.Diagnostics.Debug.WriteLine(e.ToString());                
        }
        finally
        {
            MessagingCenter.Send<DownloadCompletedMessage>(new DownloadCompletedMessage(sourceUrl, filePathWhereToSave, error, isCancelled), MessageNameConstants.DownloadCompletedMessage);

            if (downloadingTasks.ContainsKey(sourceUrl))
                downloadingTasks.Remove(sourceUrl);
        }
    }       
4

1 に答える 1