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 move the 'Remove file' above
the '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, but allow full access in the current, or an
atomic Close and Delete operation. It also needs to work across a
Windows file shared network using the UNC
Thank you,
Kind regards,
Eliott 5 4065
Hi Elliot,
Try experimenting with a lock file and have each process check for the existance of a <Filename>.lock which would indicate that even though the file may be accessible, it should not be accessed. Once the file has been removed, remove the lock file as well.
--
Happy coding!
Morten Wennevik [C# MVP]
Darn it, sorry for the double-post of 'File lock'. Posted through Google and
it didn't show up after a day, so I thought it must have failed
Kind regards,
Eliott
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tvntr5k4dj93y5@stone...
Hi Elliot,
Thank you for the response Morten Wennevik,
Try experimenting with a lock file and have each process check for the
existance of a <Filename>.lock which would indicate that even though the
file may be accessible, it should not be accessed. Once the file has been
removed, remove the lock file as well.
This is almost perfect, except that if the process that gains access to
the file crashes mid-processing, <Filename>.lock will not have a chance to
be deleted. Then the file would remain locked until a user deleted the .lock
manually.
I almost get it to work with File.Open(.., FileShare.Delete) because I can
delete the file while it's still locked and not crashed, plus, if the
program does crash, the OS takes care of the unlocking the file (only
problem is that someone could come along and delete the file
mid-processing).
If it was possible in .NET to create the .lock file so that it
automatically deleted itself when it was closed (like Win32's CreateFile(..,
FILE_FLAG_DELETE_ON_CLOSE); ) then this would work great.
--
Happy coding!
Morten Wennevik [C# MVP]
Thanks again,
Kind regards,
Eliott
On Thu, 19 Jul 2007 00:50:51 +0200, MRe <m.r.e.@.d.u.b.l.i.n...i.ewrote:
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tvntr5k4dj93y5@stone...
>Hi Elliot,
Thank you for the response Morten Wennevik,
>Try experimenting with a lock file and have each process check for the existance of a <Filename>.lock which would indicate that even though the file may be accessible, it should not be accessed. Once the file hasbeen removed, remove the lock file as well.
This is almost perfect, except that if the process that gains accessto
the file crashes mid-processing, <Filename>.lock will not have a chance to
be deleted. Then the file would remain locked until a user deleted the.lock
manually.
I almost get it to work with File.Open(.., FileShare.Delete) becauseI can
delete the file while it's still locked and not crashed, plus, if the
program does crash, the OS takes care of the unlocking the file (only
problem is that someone could come along and delete the file
mid-processing).
If it was possible in .NET to create the .lock file so that it
automatically deleted itself when it was closed (like Win32's CreateFile(..,
FILE_FLAG_DELETE_ON_CLOSE); ) then this would work great.
>-- Happy coding! Morten Wennevik [C# MVP]
Thanks again,
Kind regards,
Eliott
Well, if you can do it in Win32, you can probably do it in .Net using pinvoke (see www.pinvoke.net)
In case of FILE_FLAG_DELETE_ON_CLOSE I did I quick test and the code below may be what you need. It will create a lock file when a winform loads and the lock file vanishes when the form is closed.
protected override void OnLoad(EventArgs e)
{
IntPtr hFile = Win32.CreateFile(
"C:\\Test.lock",
Win32.GENERIC_READ,
0,
IntPtr.Zero,
Win32.CREATE_NEW,
Win32.FILE_FLAG_DELETE_ON_CLOSE,
IntPtr.Zero);
}
class Win32
{
//DesiredAccess
public const UInt32 GENERIC_READ = 0x80000000;
public const UInt32 GENERIC_WRITE = 0x40000000;
//ShareMode
public const UInt32 FILE_SHARE_READ = 0x00000001;
public const UInt32 FILE_SHARE_WRITE = 0x00000002;
//CreationDisposition
public const UInt32 CREATE_NEW = 1;
public const UInt32 CREATE_ALWAYS = 2;
public const UInt32 OPEN_EXISTING = 3;
public const UInt32 OPEN_ALWAYS = 4;
public const UInt32 TRUNCATE_EXISTING = 5;
public const UInt32 FILE_FLAG_DELETE_ON_CLOSE = 0x04000000;
[DllImport("Kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateFile(
String lpFileName,
UInt32 dwDesiredAccess,
UInt32 dwShareMode,
IntPtr lpSecurityAttributes,
UInt32 dwCreationDisposition,
UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile);
}
--
Happy coding!
Morten Wennevik [C# MVP]
Hi Morten Wennevik,
Wow, thank you very much for all this code :) looks good. I will give it a
shot
Thank you again,
Kind regards,
Eliott
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tvplo3nqdj93y5@stone...
On Thu, 19 Jul 2007 00:50:51 +0200, MRe <m.r.e.@.d.u.b.l.i.n...i.ewrote:
>[..]
>Try experimenting with a lock file and have each process check for the existance of a <Filename>.lock which would indicate that even though the file may be accessible, it should not be accessed. Once the file has been removed, remove the lock file as well.
[..]
If it was possible in .NET to create the .lock file so that it
automatically deleted itself when it was closed (like Win32's
CreateFile(..,
FILE_FLAG_DELETE_ON_CLOSE); ) then this would work great.
[..]
Well, if you can do it in Win32, you can probably do it in .Net using
pinvoke (see www.pinvoke.net)
In case of FILE_FLAG_DELETE_ON_CLOSE I did I quick test and the code below
may be what you need. It will create a lock file when a winform loads and
the lock file vanishes when the form is closed.
protected override void OnLoad(EventArgs e)
{
IntPtr hFile = Win32.CreateFile(
"C:\\Test.lock",
Win32.GENERIC_READ,
0,
IntPtr.Zero,
Win32.CREATE_NEW,
Win32.FILE_FLAG_DELETE_ON_CLOSE,
IntPtr.Zero);
}
class Win32
{
//DesiredAccess
public const UInt32 GENERIC_READ = 0x80000000;
public const UInt32 GENERIC_WRITE = 0x40000000;
//ShareMode
public const UInt32 FILE_SHARE_READ = 0x00000001;
public const UInt32 FILE_SHARE_WRITE = 0x00000002;
//CreationDisposition
public const UInt32 CREATE_NEW = 1;
public const UInt32 CREATE_ALWAYS = 2;
public const UInt32 OPEN_EXISTING = 3;
public const UInt32 OPEN_ALWAYS = 4;
public const UInt32 TRUNCATE_EXISTING = 5;
public const UInt32 FILE_FLAG_DELETE_ON_CLOSE = 0x04000000;
[DllImport("Kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateFile(
String lpFileName,
UInt32 dwDesiredAccess,
UInt32 dwShareMode,
IntPtr lpSecurityAttributes,
UInt32 dwCreationDisposition,
UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile);
}
--
Happy coding!
Morten Wennevik [C# MVP] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pekka Niiranen |
last post by:
Hi,
I have used the following example from win32 extensions:
-----SCRIPT STARTS----
import win32file
import win32con
import win32security
import pywintypes
|
by: KDB |
last post by:
Hi,
I am trying the following code to get a write lock on a file.
#include <unistd.h>
#include <iostream.h>
#include <fcntl.h>
main()
{
int fd = open("file",O_RDWR);
|
by: Stephen Corey |
last post by:
I'm writing an app that basically just appends text to a text file on a
Win2K Server. They fill out a form, click a button, and 1 line is
appended to the file. Multiple people will run this app at...
|
by: martin |
last post by:
Hi,
I have noticed that every aspx page that I created (and ascx file) has an
assosiated resource file aspx.resx.
However what I would like to do is have a single global resource file for
the...
|
by: Gary Nelson |
last post by:
Anyone have any idea why this code does not work?
FileOpen(1, "c:\JUNK\MYTEST.TXT", OpenMode.Binary,
OpenAccess.ReadWrite, OpenShare.Shared)
Dim X As Integer
For X = 1 To 26
FilePut(1, Chr(X +...
|
by: muttu2244 |
last post by:
hi all
am trying to write some information into the file, which is located in
ftp, and this file can be updated by number of people, but if at all i
download a file from the ftp to my local...
|
by: ABCL |
last post by:
Hi All,
I am working on the situation where 2 different
Process/Application(.net) tries to open file at the same time....Or one
process is updating the file and another process tries to access...
|
by: George |
last post by:
Hi,
I am re-writing part of my application using C#. This application starts
another process which execute a "legacy" program. This legacy program writes
to a log file and before it ends, it...
|
by: WingSiu |
last post by:
I am writing a Logging util for my ASP.NET application.
I am facing mulit process problem.
I developed a class LogFactory, and have a method called Get_Logger to
create a FileLogger, which will...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |