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. Can a file be opened?

Can a file be opened?

Scheduled Pinned Locked Moved C#
csharpc++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.
  • _ Offline
    _ Offline
    __DanC__
    wrote on last edited by
    #1

    I have some code which has to be able to know whether a file can be opened, if so, open it, if not, try again. In C++ I would use CreateFile() and test if the returned handle was valid. In C# it looks like I would have to use new FileStream() and wait for an exception to be thrown which seems to slow. Is there any way I can instantly tell if a file can be opened?

    A 1 Reply Last reply
    0
    • _ __DanC__

      I have some code which has to be able to know whether a file can be opened, if so, open it, if not, try again. In C++ I would use CreateFile() and test if the returned handle was valid. In C# it looks like I would have to use new FileStream() and wait for an exception to be thrown which seems to slow. Is there any way I can instantly tell if a file can be opened?

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      File.Open Although it is basically the same thing. Waiting for the exception will not be slow though. You try to open the file, and if it can't it tells you so in the form of an exception. If no exception is thrown then you know the file has been opened succesfully. It's probably much better than calling CreateFile then checking the handle.

      __DanC__ wrote:

      Is there any way I can instantly tell if a file can be opened?

      The only way is to try and open it. How do you know if a door is locked if you don't try to open it first, psychic powers?

      My current favourite word is: I'm starting to run out of fav. words!

      -SK Genius

      Game Programming articles start -here[^]-

      _ 1 Reply Last reply
      0
      • A Anthony Mushrow

        File.Open Although it is basically the same thing. Waiting for the exception will not be slow though. You try to open the file, and if it can't it tells you so in the form of an exception. If no exception is thrown then you know the file has been opened succesfully. It's probably much better than calling CreateFile then checking the handle.

        __DanC__ wrote:

        Is there any way I can instantly tell if a file can be opened?

        The only way is to try and open it. How do you know if a door is locked if you don't try to open it first, psychic powers?

        My current favourite word is: I'm starting to run out of fav. words!

        -SK Genius

        Game Programming articles start -here[^]-

        _ Offline
        _ Offline
        __DanC__
        wrote on last edited by
        #3

        That's what I thought, it just seemed slower when catching the exception and retrying.

        P 1 Reply Last reply
        0
        • _ __DanC__

          That's what I thought, it just seemed slower when catching the exception and retrying.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Read this[^]

          _ 1 Reply Last reply
          0
          • P PIEBALDconsult

            Read this[^]

            _ Offline
            _ Offline
            __DanC__
            wrote on last edited by
            #5

            Thanks for the link but even the referenced articles state that exception handling should not be used used in normal application flow, they should be as the name says "exceptions" whereas in my application it is perfectly possible that another process or thread may have the file open and so I need to retry until it becomes available. On a side note, when using FileStreams it also seems that the internal file handle is not closed immediately (they are used within a using block) as I get a lot of exceptions opening files that were previously open and are no longer open.

            A 1 Reply Last reply
            0
            • _ __DanC__

              Thanks for the link but even the referenced articles state that exception handling should not be used used in normal application flow, they should be as the name says "exceptions" whereas in my application it is perfectly possible that another process or thread may have the file open and so I need to retry until it becomes available. On a side note, when using FileStreams it also seems that the internal file handle is not closed immediately (they are used within a using block) as I get a lot of exceptions opening files that were previously open and are no longer open.

              A Offline
              A Offline
              Anthony Mushrow
              wrote on last edited by
              #6

              If the file is opened within a thread you control, then perhaps you could set something up to track which files you have open. Then you could just check against the tracker. Perhaps this tracker could also fire events so that other threads wouldn't have to keep checking to see if the file is available. Maybe even a waiting list so you can see how many threads are waiting to open a file, you could even prioritize the list.... the possibilities are endless. But err, maybe just a list of files you have open.

              My current favourite word is: I'm starting to run out of fav. words!

              -SK Genius

              Game Programming articles start -here[^]-

              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