Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
What is the best way to execute file opening asynchronously even before copying begins. The exception I get is that the file is not accessible because its being used by another process.My code is shown below:

C#
try
               {
                   using (var sourceStream = new FileStream(pathToDownloadedMedia, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 4096, useAsync: true))
                   {
                       using (var destinationStream = File.Create(pathToProcessedMedia))
                       {
                           await sourceStream.CopyToAsync(destinationStream);
                       }
                   }
               }
               catch (Exception)
               {
                   throw new ApplicationException();
               }
Posted

1 solution

By using FileShare.None you explicitly define the behaviour that you get.

Better use FileShare.ReadWrite and see what happens.

Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900