I got some intermit exceptions to write texts to a brand new file in one of my UWP application. The sample codes look like
var myFolder = await GetFolder("MyFolderName");
var fileName = $"{random}-{DateTime.Now.ToString("yyyyMMddHHmmss")}.xml";
var myFile = await myFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(myFile, "some content");
private static async Task<StorageFolder> GetFolder(string folderName)
{
var storageFolder = ApplicationData.Current.LocalFolder;
var possibleFolder = await storageFolder.TryGetItemAsync(folderName);
if (possibleFolder == null)
{
await storageFolder.CreateFolderAsync(folderName);
}
var folder = await storageFolder.GetFolderAsync(folderName);
return folder;
}
The exception is thrown on line await FileIO.WriteTextAsync(myFile, "some content");
with the following details
System.IO.FileLoadException: 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. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at MyClass.<>c__DisplayClass6_0.<b__0>d.MoveNext()
I really don't get, how can a brand new file being used already?