Akbar Shaik
Akbar Shaik

Reputation: 9

C# System.IO : The process cannot access the file path because it is being used by another process

I am trying to debug the code for uploading the .xlsx file through my application in my local system. At the time of appending log data to the log file this exception

The process cannot access the file'C:\\Webconfigfiles\\Upload\\Save\\Log_20230706050233.txt' because it is being used by another process.

My doubt is that some of the data is already appended to the log file. (I have added a screenshot of the log file)

However, the rest of the log methods are throwing errors when appending data to my log file.

I cleared the application pool in my local IIS server, but it's still throwing the same error message. I have implemented dispose method by using statement at the time of creating appending log file. I have placed my code below

public Logger(string savePath)
{
    filePath = savePath + @"Log_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt";
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Create)))
    {
        writer.WriteLine("                                                             ");
        writer.WriteLine("-------------------------------------------------------------");
        writer.WriteLine(DateTime.Now.ToString());
        writer.WriteLine("-------------------------------------------------------------");
        writer.Close();
        //logFileStream.Close();
    }

}
public void WriteToLogFile(string Table, string Row, string refTable)
{
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Append)))
    {
        //logFileStream = new FileStream(filePath, FileMode.Append);
        //logStreamWriter = new StreamWriter(logFileStream);
        writer.WriteLine("{0} table - Column value for {2} in row {1} of {0} table does not exist in {2} table", Table, Row, refTable);
        writer.Close();
        writer.Close();
    }
}
public void WriteToLogFileWithAppError(string Table, string Row, string Error)
{
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Append)))
    {
        writer.WriteLine("[{0} :] {1} table - {1} in row {2} with Error Message: {3}", DateTime.Now, Table, Row, Error);
        writer.Close();
        writer.Close();
    }

}
public void WriteToLogFileWithGeneralError(string Error)
{
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Append)))
    {
        writer.WriteLine("");
        writer.WriteLine("[{0} :] Error Message: {1}", DateTime.Now, Error);
        writer.Close();
        writer.Close();
    }
}
public void WriteToLogFile(string msg)
{
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Append)))
    {
        writer.WriteLine("");
        writer.WriteLine("{0}", msg);
        writer.Close();
        writer.Close();
    }
}

Upvotes: 0

Views: 230

Answers (1)

Nick
Nick

Reputation: 5042

You close the StreamWriter, but you must close the stream as well. Revise your code like this:

 using (var stream new FileStream(filePath, FileMode.Append)) {
    using (var writer = new StreamWriter(stream)
    {
        writer. WriteLine("");
        writer.WriteLine("{0}", msg);
    }
 }

Upvotes: 0

Related Questions