question

LoganStach-1629 avatar image
0 Votes"
LoganStach-1629 asked ·

How do I make sure a file is closed before attempting to read to it? (UWP)

I have a UWP app where a method is invoked that reads and writes the necessary data. It is reads when the page loads or the program opens, and writes data when the page unloads and the app closes. It looks like this:

 async void WriteAndRead(bool write)
     {
         //Writes data if the passed boolean is true
         if (write == true)
         {
             Windows.Storage.StorageFolder storageFolder =
                     Windows.Storage.ApplicationData.Current.LocalFolder;
        
                     string json = JsonConvert.SerializeObject(savedFoodsList);
                     Windows.Storage.StorageFile savedFoodFile = await 
                     storageFolder.GetFileAsync("savedFood.txt");
                     await Windows.Storage.FileIO.WriteTextAsync(savedFoodFile, json);
         }
         //reads data if the passed boolean if false
         else
         {
              Windows.Storage.StorageFolder storageFolder =
                      Windows.Storage.ApplicationData.Current.LocalFolder;
        
                     Windows.Storage.StorageFile savedFoodFile = await 
                     storageFolder.GetFileAsync("savedFood.txt");
    
                     Object value = await 
                      Windows.Storage.FileIO.ReadTextAsync(savedFoodFile);
         }

While this code saves the data that it needs to, if I invoke WriteAndRead(true) too soon after WriteAndRead(false) is invoked, I get an error message that states, "The process cannot access the file because it is being used by another process. The file is in use. Please close the file before continuing.". But if I wait a couple of moments before I invoke WriteAndRead(true), it works just fine, presumably because the async method finishes up.

How do I make it so that the file is closed by the reading process before the program attempts to write to it?

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered ·

How do I make sure a file is closed before attempting to read to it? (UWP)

I checked your code, WriteAndRead is async void method. You could await void method, so we could not make sure write process is finished. please try to edit your method with Task return type and call it with await key word.


 public async Task WriteAndRead(bool write)
 {
      
     //Writes data if the passed boolean is true
     if (write == true)
     {
         Windows.Storage.StorageFolder storageFolder =
                 Windows.Storage.ApplicationData.Current.LocalFolder;
    
         string json = JsonConvert.SerializeObject(savedFoodsList);
         Windows.Storage.StorageFile savedFoodFile = await
         storageFolder.CreateFileAsync("savedFood.txt",Windows.Storage.CreationCollisionOption.OpenIfExists);
         await Windows.Storage.FileIO.WriteTextAsync(savedFoodFile, json);
     }
     //reads data if the passed boolean if false
     else
     {
         Windows.Storage.StorageFolder storageFolder =
                 Windows.Storage.ApplicationData.Current.LocalFolder;
    
         Windows.Storage.StorageFile savedFoodFile = await
         storageFolder.GetFileAsync("savedFood.txt");
    
         Object value = await
          Windows.Storage.FileIO.ReadTextAsync(savedFoodFile);
     }
 }


· 4 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So do I just apply the change you made and then call it using await WriteAndRead()?

0 Votes 0 · ·

yep, and add Task before WriteAndRead method

0 Votes 0 · ·

Maybe I'm not doing this right, but I tried what you said and now nothing is getting saved at all. I changed WriteAndRead() to a public async task, and I use await WriteAndRead(false) at the Page_Loaded event, and await WriteAndRead(true) at the App_CloseRequested and Page_Unloaded events, but nothing appears to be being written to the file. Is there something that I'm missing?

0 Votes 0 · ·
Show more comments