0

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

abydal
  • 378
  • 1
  • 8
Reza Del
  • 763
  • 10
  • 30
  • 1
    When you are writing to a file for each web request, you need to handle the race condition that can occur. If two requests are more or less simultaneously both of them will try to write to the file at the same time. – abydal Aug 04 '17 at 11:25
  • no need to check if Dir exists. Directory.CreateDirectory will either return existing dir or create a new one (if the path is valid) – Jury Golubev Aug 04 '17 at 12:13
  • Where are you calling this function from? – martennis Aug 04 '17 at 12:30
  • they are being called in 2 places, 1 when the API get the request, 2 when the request is process and before the request result is sent back – Reza Del Aug 04 '17 at 13:06
  • @abydal Thank you, i did not know what was race condition and now that i know, i am locking the file save and the issue has been resolved – Reza Del Aug 04 '17 at 13:49

1 Answers1

3

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

Reza Del
  • 763
  • 10
  • 30