Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Best way of knowing when a file is locked?

Best way of knowing when a file is locked?

Scheduled Pinned Locked Moved C#
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neverbirth
    wrote on last edited by
    #1

    Just as the subject says... which is the best way of knowing when a file is locked by another application? the only way I know to do this is trying to open and work with the file and catching the exception, and wonder if there is any other faster method. Plus if I'd wish to work with several files at once where each one depends on the previous one I'd have to use nested try...catch blocks, and you know, I appreciate my eyes.

    D L 2 Replies Last reply
    0
    • N Neverbirth

      Just as the subject says... which is the best way of knowing when a file is locked by another application? the only way I know to do this is trying to open and work with the file and catching the exception, and wonder if there is any other faster method. Plus if I'd wish to work with several files at once where each one depends on the previous one I'd have to use nested try...catch blocks, and you know, I appreciate my eyes.

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Here[^]

      N 1 Reply Last reply
      0
      • D dan sh

        Here[^]

        N Offline
        N Offline
        Neverbirth
        wrote on last edited by
        #3

        I guess you are telling me to plain use a FileSystemWatcher, because using it together with a service is something just not feasible when any file on the computer may be open. Will this work? Always used FileSystemWatcher for being aware about changes in a file or folder since it starts working, I didn't know it could give information of the current status of a file. Anyway, wouldn't this need a lot of resources for just trying to open a file? Well, I'll give it a look when I return at home.

        1 Reply Last reply
        0
        • N Neverbirth

          Just as the subject says... which is the best way of knowing when a file is locked by another application? the only way I know to do this is trying to open and work with the file and catching the exception, and wonder if there is any other faster method. Plus if I'd wish to work with several files at once where each one depends on the previous one I'd have to use nested try...catch blocks, and you know, I appreciate my eyes.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          When you open the filestream, request non shared access, it will fail then, and not later.

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          N 1 Reply Last reply
          0
          • L leppie

            When you open the filestream, request non shared access, it will fail then, and not later.

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 4a out now (29 May 2008)

            N Offline
            N Offline
            Neverbirth
            wrote on last edited by
            #5

            Well, that's what I'm already doing. I'd like to know if there is some function (I don't care if it isn't a managed one) that directly tells if a file is locked by another application, that way I wouldn't need to use nested try...catch blocks.

            L 1 Reply Last reply
            0
            • N Neverbirth

              Well, that's what I'm already doing. I'd like to know if there is some function (I don't care if it isn't a managed one) that directly tells if a file is locked by another application, that way I wouldn't need to use nested try...catch blocks.

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Neverbirth wrote:

              that way I wouldn't need to use nested try...catch blocks.

              Why do you need that? Just refactor the file opening process to a method and handle this specific scenario.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups