Error on DeleteAsync fileStorage in windows fall creation update

0

With the upgrade to windows fall creation my application has a problem, when a file is deleted, it gives me an exception:

System.IO.FileLoadException: 'The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)'

The file is first copied to my directory and then deleted:

protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        if (e.Parameter != null)
        {
             folder = ApplicationData.Current.LocalFolder;
             this.myfile= e.Parameter as MyFile;

             this.view = await this.myfile.CopyAsync(folder, this.myfile.Name, NameCollisionOption.ReplaceExisting);
        }
    }

and before leave the view:

 protected override async void OnNavigatedFrom(NavigationEventArgs e)
    {
        await this.view.DeleteAsync();
    }

In windows creation update this work, in windows fall creation update not work.

Update: I have analyzed the error, this is due to the use of the PdfDocument library.

var folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync("temp.pdf");
using (IRandomAccessStream filestream = await file.OpenAsync(FileAccessMode.ReadWrite))
{

}
var pdfDocument = await PdfDocument.LoadFromFileAsync(file);
using (IRandomAccessStream filestream1 = await file.OpenAsync(FileAccessMode.ReadWrite))
{

}
windows
uwp
windows-10
asked on Stack Overflow Oct 23, 2017 by Andrea485 • edited Nov 5, 2017 by Andrea485

1 Answer

0

the error occurs only when I open the file with OpenAsync (File Access Mode.ReadWrite); , if I open the view twice with the same file I get an error

If you mean open the file twice without disposing the first stream as the following code snippet showed, it do will throw the exception as you described in the thread.

await temp.OpenAsync(FileAccessMode.ReadWrite);
await temp.OpenAsync(FileAccessMode.ReadWrite);

maybe I need a method to close the file?

As you considered, you need to call the dispose method to allow the resources used by the Stream to be reallocated for other purposes. The stream will not be auto disposed until you invoke the dispose method, so that you cannot open the file stream again. For example, you should update your code snippet to:

StorageFile temp = await ApplicationData.Current.LocalFolder.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting);
IRandomAccessStream stream = await temp.OpenAsync(FileAccessMode.ReadWrite);
stream.Dispose(); 
IRandomAccessStream streamtwice= await temp.OpenAsync(FileAccessMode.ReadWrite);
streamtwice.Dispose();

Using statement provides a convenient syntax that ensures the correct use of IDisposable objects. It is recommended to use using statement instead, code as follows:

StorageFile temp = await ApplicationData.Current.LocalFolder.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream = await temp.OpenAsync(FileAccessMode.ReadWrite))
{
}
answered on Stack Overflow Oct 31, 2017 by Sunteen Wu

User contributions licensed under CC BY-SA 3.0