0

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;
        }
    }
Steeeve
  • 824
  • 1
  • 6
  • 14
patrickgc
  • 49
  • 7
  • Try using the [using clause pattern for IDisposable](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/using-statement) to be sure to free up Windows resources and handle, thus any own lock. –  Aug 07 '21 at 10:21
  • 1
    Does this answer your question? [File being used by another process after using File.Create()](https://stackoverflow.com/questions/2781357/file-being-used-by-another-process-after-using-file-create) and [Release resources in .Net C#](https://stackoverflow.com/questions/2764075/release-resources-in-net-c-sharp) –  Aug 07 '21 at 10:23
  • @OlivierRogier Same issue with using(StreamWriter sw = new StreamWriter(_fullPath, true)) { } – patrickgc Aug 07 '21 at 10:34
  • I've faced this problem with some trashy antivirus programs. After a file is closed they scan the file and you can't read it immediately. – Steeeve Aug 07 '21 at 10:37
  • Does the file exist before creating it? In all cases, put a breakpoint on the reading line and before executing it what does lockhunter say? –  Aug 07 '21 at 10:46
  • 1
    Lockhunter says that the file is blocked by its own application. – patrickgc Aug 07 '21 at 11:06
  • 1
    I must apologize, the mistake was between the ears. In the method GetFullPath, I already check if the file exists and create it if necessary. I still thank everyone for the support. – patrickgc Aug 07 '21 at 11:16

1 Answers1

0

You create an empty file to deserialize it afterwards. You can perform deserialization only if the file exists:

public SettingsModel LoadSettings()
{
    var _fullPath = FileHelper.GetFullPath(_fileName);
    var settings = File.Exists(_fullPath) 
        ? JsonConvert.DeserializeObject<SettingsModel>(File.ReadAllText(_fullPath))
        : new SettingsModel();
    return settings;
}

Michael
  • 1,166
  • 5
  • 4
  • While this is ok, the real problem is, that you can't read from a file immediatly after creation (*However, I always get an exception that says that the process cannot access the file*) – Steeeve Aug 07 '21 at 10:41
  • File.Create returns an (open) stream, so you have to close the returned Stream before reading: `using (var fs = File.Create("myFile.txt")) { } var content = File.ReadAllText("myFile.txt");` – Michael Aug 07 '21 at 10:48
  • `File.Create(...).Close()` also closes the file. I would also put that in a using block instead, but in this case, the file can't be left open. If File.Create fails, there is no stream to close. – Steeeve Aug 07 '21 at 10:54
  • Unfortunately, this does not solve my problem either. – patrickgc Aug 07 '21 at 11:07
  • I must apologize, the mistake was between the ears. In the method GetFullPath, I already check if the file exists and create it if necessary. I still thank everyone for the support. – patrickgc Aug 07 '21 at 11:16
  • You can use *Path.GetFullPath(filename)* from System.IO namespace directly ;) – Michael Aug 07 '21 at 11:23