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);
}
}