fCraft.Paths.TestFile C# (CSharp) Method

TestFile() public static method

Makes sure that the path format is valid, and optionally whether it is readable/writeable.
public static TestFile ( [ fileLabel, [ filename, bool createIfDoesNotExist, FileAccess neededAccess ) : bool
fileLabel [ Name of the path that's being tested (e.g. "map path"). Used for logging.
filename [ Full or partial path of the file.
createIfDoesNotExist bool If target file is missing and this option is OFF, TestFile returns true. /// If target file is missing and this option is ON, TestFile tries to create /// a file and returns whether it succeeded.
neededAccess FileAccess If file is present, type of access to test.
return bool
        public static bool TestFile( [NotNull] string fileLabel, [NotNull] string filename,
            bool createIfDoesNotExist, FileAccess neededAccess)
        {
            if ( fileLabel == null )
                throw new ArgumentNullException( "fileLabel" );
            if ( filename == null )
                throw new ArgumentNullException( "filename" );
            try {
                new FileInfo( filename );
                if ( File.Exists( filename ) ) {
                    if ( ( neededAccess & FileAccess.Read ) == FileAccess.Read ) {
                        using ( File.OpenRead( filename ) ) { }
                    }
                    if ( ( neededAccess & FileAccess.Write ) == FileAccess.Write ) {
                        using ( File.OpenWrite( filename ) ) { }
                    }
                } else if ( createIfDoesNotExist ) {
                    using ( File.Create( filename ) ) { }
                }
                return true;
            } catch ( Exception ex ) {
                if ( ex is ArgumentException || ex is NotSupportedException || ex is PathTooLongException ) {
                    Logger.Log( LogType.Error,
                                "Paths.TestFile: Specified path for {0} is invalid or incorrectly formatted ({1}: {2}).",
                                fileLabel, ex.GetType().Name, ex.Message );
                } else if ( ex is SecurityException || ex is UnauthorizedAccessException ) {
                    Logger.Log( LogType.Error,
                                "Paths.TestFile: Cannot create or write to {0}, please check permissions ({1}: {2}).",
                                fileLabel, ex.GetType().Name, ex.Message );
                } else if ( ex is DirectoryNotFoundException ) {
                    Logger.Log( LogType.Error,
                                "Paths.TestFile: Drive/volume for {0} does not exist or is not mounted ({1}: {2}).",
                                fileLabel, ex.GetType().Name, ex.Message );
                } else if ( ex is IOException ) {
                    Logger.Log( LogType.Error,
                                "Paths.TestFile: Specified file for {0} is not readable/writable ({1}: {2}).",
                                fileLabel, ex.GetType().Name, ex.Message );
                } else {
                    throw;
                }
            }
            return false;
        }