After creating a file via File.Create, I want to read it afterwards with File.ReadAllText. However, I always get an exception that says that the process cannot access the file. Once the file is created, access works without problems. So I assume that the file is not yet released by File.Create at the time where it should be read. How do I solve this? Below is my method.
public SettingsModel LoadSettings()
{
var _fullPath = FileHelper.GetFullPath(_fileName);
if (!File.Exists(_fullPath))
{
File.Create(_fullPath).Close();
}
var serializedSettings = File.ReadAllText(_fullPath);
var settings = JsonConvert.DeserializeObject<SettingsModel>(serializedSettings);
if (settings == null)
{
return new SettingsModel();
}
else
{
return settings;
}
}