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

Logan Stach 41 Reputation points
2020-01-27T22:29:58.87+00:00

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?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points
    2020-01-28T01:27:40.09+00:00

    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 can't 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);  
        }  
    }