473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Atomic file-sustitution in .NET?

On unix-based systems i use (the libc function) "rename" to do atomic
replacement of one file with another.

How would I achieve the same using .NET primitives?

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Sep 7 '05 #1
4 2023
Helge Jensen wrote:
On unix-based systems i use (the libc function) "rename" to do atomic
replacement of one file with another.

How would I achieve the same using .NET primitives?


There are two Windows API functions that might be useful: ReplaceFile and
MoveFileEx. Both seem to be able to replace a file if used with the
correct parameters, but none of them guarantee atomicity. I don't think
any of them is available via .NET framework classes.

I googled for "windows posix rename" and I came up with a few mailing list
threads discussing the issue, with the general consent being that Windows
can't do it - many things in Windows file handling aren't very POSIX
compatible, after all.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Sep 7 '05 #2


Oliver Sturm wrote:
I googled for "windows posix rename" and I came up with a few mailing
list threads discussing the issue, with the general consent being that
Windows can't do it - many things in Windows file handling aren't very
Unfortunatly , this is what I suspected :(
POSIX compatible, after all.


I'm not really too interested in whether the behaviour is POSIX, but in
mechanisms to guarantee consistency during updates. I guess ill have to
live with the race-condition and data-loss possibility of the pattern:

if ( File.Exists(path) )
File.Remove(path);
File.Move(path, newpath);

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Sep 7 '05 #3
Helge Jensen wrote:
I'm not really too interested in whether the behaviour is POSIX, but in
mechanisms to guarantee consistency during updates. I guess ill have to
live with the race-condition and data-loss possibility of the pattern:

if ( File.Exists(path) )
File.Remove(path);
File.Move(path, newpath);


There's no way around that pattern, I think. But it should read:

if ( File.Exists(newpath) )
File.Remove(newpath);
File.Move(path, newpath);
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Sep 7 '05 #4
There's no way around that pattern, I think. But it should read:

if ( File.Exists(newpath) )
File.Remove(newpath);
File.Move(path, newpath);


Thanks, for pointing out my typo ;)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Sep 7 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
1
by: Kashish | last post by:
Is file<<"Some string"<<endl is an atomic operation. where file is an ofstream object. If we output a string in ofstream and we provide endl after that,Can we make sure the whole string will be...
5
by: hikums | last post by:
Begin Atomic Declare a integer; Set a=(select count(1) from claims.table1); if a<>0 then import into c:\tmptyfile.txt of del replace into claims.table1; end if; End!
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
1
by: CptDondo | last post by:
I have an embedded system that will save configuration files based on user input. This device will be used in areas that have uncertain power, and a battery backup is not a realistic option. It...
1
by: spacehopper_man | last post by:
no "rename" operation in C# !!! - this has been covered in this group before, but I can't find any good answers. what I am trying to do is refresh the content in a file with minimum...
11
by: japhy | last post by:
Is there a way to read a line (a series of characters ending in a newline) from a file (either by descriptor or stream) atomically, without buffering additional contents of the file?
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
11
by: Jon Harrop | last post by:
Can read locks on a data structure be removed safely when updates are limited to replacing a reference? In other words, is setting a reference an atomic operation? I have been assuming that all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.