File in use

T

tshad

I am opening a file in my .aspx page like so:

// Pass the file path and the file name to the StreamWriter constructor.
objStreamWriter = new StreamWriter(Server.MapPath("Employee.txt"));

If I should kill the page and restart the page I get the following message:

The process cannot access the file
"c:\inetpub\wwwroot\integration\Employee.txt" because it is being used by
another process.

If I wait long enough (about 30 seconds), I can run the page without the
error.

I assume this is because even though the page has died, the Post Back is
still running, but if the Post Back should die for some reason and the file
does not get closed, it will stay in use. Is there a way to override the
"in use" status and delete the file without stopping and restarting the web
server?

Thanks,

Tom
 
O

Onwuka Emeka

Make sure your StreamWriter is closed after using it putting the close in a
finally block might be more suitable

StreamWriter writer = null;
try{
writer = new StreamWriter("some file name");
//...do stuff with your writer;
}
catch(IOException e)
{
//handle the exception
}
finally{
if(writer != null)
writer.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top