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;
}