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();
}

Recommended Answers

All 6 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.