File lock

P

pgdown

Hi,

I have several processes accessing files from one folder, but only
one process should ever access each file. Once one process has the
file, no other process should be allowed to access it, even after the
first process is finished with it, except in the case where the first
process crashes. In pseudo code..

* Open file with exclusive lock
* Process file - failure will throw exception, skipping 'Remove
file' step below
* Close file and unlock
* Remove file - to clean up, but also to prevent other processes now
accessing the file

...So far I cannot think of a way of doing this, taking into account
pathological cases like the file being accessed between 'Close file
and unlock' and 'Remove file'. The closest I've come however, is to
use File.Open(.. FileShare.Delete), so I can switch 'Remove file' with
'Close file and unlock', but then there the problem that someone could
come along and delete the file mid-processing.

Is there a possible solution in .NET? A way to keep the file locked
from other processes, and still allow full access in the current
process, or an atomic Unlock-and-Delete operation. It also needs to
work across a Windows file shared network using the UNC

Hope someone can help, I have been unable to find anything specific
on Google.

Thank you,
Kind regards,
Eliott
 
W

Wolfgang Hauer

Hi!

If you do not find another solution try what i have done 20 years ago in
good old Dos:

Create a dummy file in your directory.
Build a hash from the filename Try to lock the byteposition of the hash in
the dummy file. If failed onother process has that file.
It's like a mutex but works across the network.
The only problem is, that you have to make that in ALL your processes.

If you find a better solution please post.

HTH
Wolfgang
 
M

MRe

Wolfgang Hauer said:

Thank you for the response Wolfgang,
If you do not find another solution try what i have done 20 years ago in
good old Dos:

Create a dummy file in your directory.
Build a hash from the filename Try to lock the byteposition of the hash in
the dummy file. If failed onother process has that file.
It's like a mutex but works across the network.
The only problem is, that you have to make that in ALL your processes.

It's a nice idea, but it should perferably prevent a user deleting the
file too, which your suggestion, if I understand it correctly, wouldn't
prevent[?]
If you find a better solution please post.

I think Morten Wennevik's response, along with System.IO.File.Open(..,
FileShare.None); may do the trick.
HTH
Wolfgang

Thanks again,
Kind regards,
Eliott
 
M

MRe

I think Morten Wennevik's response, along with System.IO.File.Open(..,
FileShare.None); may do the trick.

Sorry, my thinking now is there may be a problem with this too; if I get a
solution, I'll post...

Kind regards,
Eliott
 
W

Wolfgang Hauer

Hi!:

If you have the file open in you programm nobody can delete ist.

Wolfgang
 

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