Best way of knowing when a file is locked?
-
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.
-
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.
-
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.
-
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.
-
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)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.
-
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.
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)