• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

.net detecting file in use

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    #11
    Originally posted by AtW View Post
    1) Don't
    2) Use DB
    Ever helpful.

    1) Have to
    2 Can't don't have one.

    (Granted I am inclined to agree in many ways).

    Comment


      #12
      Originally posted by eek View Post
      +1 fullautomatix's stackoverflow link is the right approach if you simply want to check if a file .

      If you have full control the unix lock file approach provides you with more information which may be useful if processes hit a snag.
      That is what I've done. The only issue that I have is if the IOException arises because of something other than a lock. It's probably OK since it does a limited number of retrys and then throws.

      However I did find this which might be useful, need to think about it because it is reliant on a specific result as a magic number so is vulnerable to change and different platform issues:-

      catch (IOException e)
      {
      return (Marshal.GetHRForException(e) & 0xFFFF) == 32;
      }

      The only other idea I could think of was to create a machine wide synchroinzation object and lock through that. In effect this means a named mutex I think. Certainly a bit of a faff.

      A final thought was to open the file differently (e.g. read write allow share) and then call "lock" - the documentation states that the only reason for the IOException this can throw is because it is locked, but then I can't assume anything about how the other process might have the file opened so that doesn't work either.

      Cheers folks.

      Comment


        #13
        Rewrite it in C using ZeroMQ.

        Comment

        Working...
        X