Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / September 2005

Tip: Looking for answers? Try searching our database.

Atomic file-sustitution in .NET?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Helge Jensen - 07 Sep 2005 09:30 GMT
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?

Signature

Helge Jensen
  mailto:helge.jensen@slog.dk
  sip:helge.jensen@slog.dk
               -=> Sebastian cover-music: http://ungdomshus.nu <=-

Oliver Sturm - 07 Sep 2005 10:09 GMT
>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
Signature

Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Helge Jensen - 07 Sep 2005 11:13 GMT
> 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);

Signature

Helge Jensen
  mailto:helge.jensen@slog.dk
  sip:helge.jensen@slog.dk
               -=> Sebastian cover-music: http://ungdomshus.nu <=-

Oliver Sturm - 07 Sep 2005 11:36 GMT
>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
[quoted text clipped - 3 lines]
>     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
Signature

Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Helge Jensen - 07 Sep 2005 11:43 GMT
> 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 ;)

Signature

Helge Jensen
  mailto:helge.jensen@slog.dk
  sip:helge.jensen@slog.dk
               -=> Sebastian cover-music: http://ungdomshus.nu <=-


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.