fCraft.Paths.TestDirectory C# (CSharp) Method

TestDirectory() public static method

Makes sure that the path format is valid, that it exists, that it is accessible and writeable.
public static TestDirectory ( [ pathLabel, [ path, bool checkForWriteAccess ) : bool
pathLabel [ Name of the path that's being tested (e.g. "map path"). Used for logging.
path [ Full or partial path.
checkForWriteAccess bool If set, tries to write to the given directory.
return bool
        public static bool TestDirectory( [NotNull] string pathLabel, [NotNull] string path, bool checkForWriteAccess )
        {
            if ( pathLabel == null )
                throw new ArgumentNullException( "pathLabel" );
            if ( path == null )
                throw new ArgumentNullException( "path" );
            try {
                if ( !Directory.Exists( path ) ) {
                    Directory.CreateDirectory( path );
                }
                DirectoryInfo info = new DirectoryInfo( path );
                if ( checkForWriteAccess ) {
                    string randomFileName = Path.Combine( info.FullName, "800Craft_write_test_" + Guid.NewGuid() );
                    using ( File.Create( randomFileName ) ) { }
                    File.Delete( randomFileName );
                }
                return true;
            } catch ( Exception ex ) {
                if ( ex is ArgumentException || ex is NotSupportedException || ex is PathTooLongException ) {
                    Logger.Log( LogType.Error,
                                "Paths.TestDirectory: Specified path for {0} is invalid or incorrectly formatted ({1}: {2}).",
                                pathLabel, ex.GetType().Name, ex.Message );
                } else if ( ex is SecurityException || ex is UnauthorizedAccessException ) {
                    Logger.Log( LogType.Error,
                                "Paths.TestDirectory: Cannot create or write to file/path for {0}, please check permissions ({1}: {2}).",
                                pathLabel, ex.GetType().Name, ex.Message );
                } else if ( ex is DirectoryNotFoundException ) {
                    Logger.Log( LogType.Error,
                                "Paths.TestDirectory: Drive/volume for {0} does not exist or is not mounted ({1}: {2}).",
                                pathLabel, ex.GetType().Name, ex.Message );
                } else if ( ex is IOException ) {
                    Logger.Log( LogType.Error,
                                "Paths.TestDirectory: Specified directory for {0} is not readable/writable ({1}: {2}).",
                                pathLabel, ex.GetType().Name, ex.Message );
                } else {
                    throw;
                }
            }
            return false;
        }

Usage Example

Example #1
0
 internal static void Init()
 {
     Paths.TestDirectory("BlockDB", Paths.BlockDBPath, true);
     Player.PlacedBlock += OnPlayerPlacedBlock;
     Scheduler.NewBackgroundTask(FlushAll).RunForever(FlushInterval, FlushInterval);
     IsEnabledGlobally = true;
 }
All Usage Examples Of fCraft.Paths::TestDirectory