write to temp file & delete it

Hi,


I'm trying to write some text to a file:


StreamWriter sw = new StreamWriter(filePath, false))
sw.Write(ticket.GetPrintString());
sw.Close();

then later i need to delete the file:

// get rid of the file
File.Delete(filePath);


but i get an exception when i try to delete:

The process can not access the file 'C:\Documents and Settings\TREVORXPS\ASPNET\LOCALS~1\Temp\tmpA3.tmp' because it is being used by another process.

how do i solve this?


thanks - Trevor
LVL 8
trevorhartmanAsked:
Who is Participating?
 
raterusCommented:
Trevor, it looks like you are doing everything correct.  As you probably know, you get errors like this when you don't close open streams to the file.  No other code touches that file before the deletion?  If you can't determine the source, head over here http://www.sysinternals.com/ntw2k/freeware/procexp.shtml, and get this utility.  You can use it to see which process has the file open (likely aspnet_wp.exe)
0
 
praneethaCommented:
i had the same problem once..and restarting it fixed it..
0
 
trevorhartmanAuthor Commented:
hmm, works now, thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.