Releasing lock on a text file?

D

David Veeneman

Is there any way to forcibly release a lock on a text file, so that it can
be deleted?

I am testing an order-processing site that saves transaction information in
a text file that it attaches to its merchant notification. In most cases, it
works fine, but occasionally a lock on the text file won't get released.
When the next transaction comes along and tries to write over the file, the
server throws an IO error, saying that the file is in use by another
application.

What I'd like to do is forcibly release the lock, so that the file can be
deleted. Is that the right way to go about it? If so, how do I do it? If
not, what would be the best approach? Thanks.

David Veeneman
Foresight Systems
 
S

sloan

You best bet is to write a good finally statement


Streamwriter sw = null;

try
{
//write to the file
Streamwriter sw = yada yada yada;
}
finally
{
if(null!=sw)
{
sw.Close();
}
}

Something like that.

the finally block runs whether an error happens or not.

If you're already doing that, then maybe give a little more detail.
 
D

David Veeneman

You best bet is to write a good finally statement

Yeah, I've got that. But for some reason I've been unable to pin down, The
lock is still not getting released.
 
A

Alvin Bruney [MVP]

In those cases, i like to use wholockme utility or filemon from sysinternals
to see what actually holds the lock on the process. It may not be the
process you think it is. At least, it certainly doesn't help to confirm it.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
 
K

kelvin

In those cases, i like to use wholockme utility or filemon from sysinternals
to see what actually holds thelockon the process. It may not be the
process you think it is. At least, it certainly doesn't help to confirm it.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon andwww.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley







- Show quoted text -

Please send me your more inquiry or requirements to my e-mail address
to (e-mail address removed)

Best regards,

Kelvin Chang
 

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

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top