Sunday, November 29, 2009

Check if a file is in use

For a project i need a method to check wether a file is open. The way i do it is very simple. I try to open the file with FileStream. See the code below:

#using System.IO

CLASS FileAccessCheck

STATIC METHOD CanOpenFile( cFile AS STRING ) AS LOGIC

    LOCAL lCanOpen        AS LOGIC
    LOCAL oStream        AS FileStream

    TRY                           
        oStream := System.IO.File.Open( cFile, FileMode.Open, FileAccess.Read, FileShare.None )
        oStream:Close()
        lCanOpen := TRUE      
    CATCH e AS System.IO.IOException      
        lCanOpen := FALSE
    END TRY      

    RETURN ( lCanOpen )

END CLASS


No comments:

Post a Comment