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