Search code examples
c#filesystemsaving-data

File being used by another process after using File.AppendAllText()


The process cannot access the file 'file path' because it is being used by another process.

i have found these 2 question

File being used by another process after using File.Create()

and

Does File.AppendAllText close the file after the operation

this is an API that i have and need to save every request that comes in and the result that goes out, there might be more than one request that a give time my code

public static void SaveTheRequestAndResponse(string type, SearchRequest searchRequest = null, dynamic result = null)
{
    var FilePath = AppDomain.CurrentDomain.BaseDirectory + @"SearchRequest";
    bool exists = Directory.Exists(FilePath);
    if (!exists)
    {
      var stream =  Directory.CreateDirectory(FilePath);
    }
    if (type == "request")
    {

        string Space = ", ";
        StringBuilder request = new StringBuilder();

        request.Append("Search Id : " + searchRequest.ID);
        request.Append(Space + "Company Name : " + searchRequest.CompanyName);
        request.Append(Space + "Country Code : " + searchRequest.CountryCode);

        request.Append(Space + "Search Type : " + searchRequest.SeacrhType);

        request.Append(Space + "Request Time : " + DateTime.Now + Environment.NewLine);

        var DataToBeSave = request.ToString();

        System.IO.File.AppendAllText(FilePath + @"\" + "FileNAme" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", DataToBeSave + Environment.NewLine);

    }
    else
    {
        string Space = ", ";
        StringBuilder SearchResult = new StringBuilder();
        SearchResult.Append("The result for Request" + Space);
        SearchResult.Append("Search Id : " + searchRequest.ID + Space);
        SearchResult.Append("States Code : " + result.StatusCode + Space);
        SearchResult.Append("Result Time : " + DateTime.Now + Environment.NewLine);

        var DataToBeSave = SearchResult.ToString();
        System.IO.File.AppendAllText(FilePath + @"\" + "FileNAme" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", DataToBeSave + Environment.NewLine);

    }
}

my understanding is that the File.AppendAllText will close after the Operation so why do i get the this error


Solution

  • my code is having an race condition, and this is because the API is being call by more than one user at each given time, even that

    System.IO.File.AppendAllText(FilePath + @"\" + "FileNAme" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", DataToBeSave + Environment.NewLine);
    

    will close after the Operation, it still need time do its work and only one connection can be open at each give time so the thread need to be lock and that can be done by

    private static Object thisLock = new Object();
    lock (thisLock)
    {
        System.IO.File.AppendAllText(FilePath + @"\" + "DandB" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", DataToBeSave + Environment.NewLine);
    }
    

    Thanks to Abydal