943,948 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2651
  • C# RSS
May 28th, 2009
0

File System C#

Expand Post »
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();
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sonya_sml is offline Offline
8 posts
since May 2009
May 28th, 2009
0

Re: File System C#

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.
Reputation Points: 10
Solved Threads: 9
Junior Poster
VIeditorlover is offline Offline
137 posts
since Dec 2007
May 28th, 2009
0

Re: File System C#

Most common is a filehandle in your own app that hasn't been closed.
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009
May 28th, 2009
0

Re: File System C#

Click to Expand / Collapse  Quote originally posted by Alxandr ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sonya_sml is offline Offline
8 posts
since May 2009
May 28th, 2009
0

Re: File System C#

c# Syntax (Toggle Plain Text)
  1. StreamWriter tw = new StreamWriter(_documentName);

If I'm not mistaken tw is a filehandle. If you forget to say [icode=c#]tw.Close()[/icode] 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).
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009
May 28th, 2009
0

Re: File System C#

Click to Expand / Collapse  Quote originally posted by Alxandr ...
c# Syntax (Toggle Plain Text)
  1. StreamWriter tw = new StreamWriter(_documentName);

If I'm not mistaken tw is a filehandle. If you forget to say [icode=c#]tw.Close()[/icode] 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sonya_sml is offline Offline
8 posts
since May 2009
May 28th, 2009
0

Re: File System C#

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.
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: stupidly simple question
Next Thread in C# Forum Timeline: Application Manager options-switches





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC