It doesn't throw this error everytime, thats what makes it strange. I got 50% chance that it works.
This is how I call the save method
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
await settings.WriteSettings(settings);
}
And this is the method itself
public async Task WriteSettings(Settings settings)
{
var ser = new System.Xml.Serialization.XmlSerializer(typeof(Settings));
var writer = new StringWriter();
ser.Serialize(writer, settings);
dom.LoadXml(writer.ToString());
StorageFolder sf = ApplicationData.Current.LocalFolder;
StorageFile st;
st = await sf.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); // here i get the error
await dom.SaveToFileAsync(st);
}
I hope you guys understand the problem and you can help me getting a solution
I got the reason why i get Acces denied when i try to Acces a file more than once. It's because i didn't close the file after using it.
This is how i acces files now:
public static async Task WriteSettings(Settings settings)
{
StorageFile file = await ApplicationData.Current.RoamingFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
var stream = await file.OpenStreamForWriteAsync();
var ser = new XmlSerializer(typeof(Settings));
ser.Serialize(stream, settings);
await stream.FlushAsync(); // This
stream.Dispose(); // And this helped me solve my problem
}