Temporary lock file access in .net

This is a discussion on Temporary lock file access in .net within the C# Programming forums, part of the General Programming Boards category; Hi, I'd like to know if there is an elegant way in .net for temporarily locking the access to a ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    58

    Temporary lock file access in .net

    Hi,
    I'd like to know if there is an elegant way in .net for temporarily locking the access to a set of files to other users.
    I can handle (copy, move etc.) something like 100 files in one shot, the files being given with wildcards in path name, so I need a simple way to process all the files and provide transaction integrity.
    Thanks

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    File.Copy() and File.Move() will internally open the source file in exclusive mode and release the lock once the operation is done. So you don't need to do anything in terms of explicitly locking the files yourself. Note however this means you should wrap it inside a Try/Catch block since if another process has that file for itself, your copy operation will throw an exception.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21