Hi,
Can someone please help me.
I am getting error: The process cannot access the file 'c:\reports\PaymentDetailReport.xml' because it is being used by another process. in following function.

This method is suppose to be called couple of times. but at second time, it throws the error. I have even closed tw.

private void saveReport(string reportName, string data)
{
if (reportName == "PaymentReport")
{
StreamWriter tw = new StreamWriter(_documentName);
tw.WriteLine(data, true);
StreamWriter masterPSDR = File.AppendText("C:\\reports\\masterPSDR.xml");
masterPSDR.WriteLine(data, true);
tw.Close();
masterPSDR.Close();
}

visit www.sysinternals.com and find filemon utility - you can monitor any filesystem activity and find what is wrong. You can download handle.exe utility (commandline) to find snapshot of opened files as well.

Most common is a filehandle in your own app that hasn't been closed.

Thanks a lot for reply.

Sorry i didn't get what you mean by filehandle not closed?

StreamWriter tw = new StreamWriter(_documentName);

If I'm not mistaken tw is a filehandle. If you forget to say tw.Close() tw is never closed and you forgot to close a filehandle. This isn't done til you exit your application (though I might be mistaken).

StreamWriter tw = new StreamWriter(_documentName);

If I'm not mistaken tw is a filehandle. If you forget to say tw.Close() tw is never closed and you forgot to close a filehandle. This isn't done til you exit your application (though I might be mistaken).

well as you see in my example, i have used tw.close(); The second last line.

Yes, but is that your whole program?
If you open that file in another function an not close it, you won't be able to open it again.

This article has been dead for over six months. Start a new discussion instead.