using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; namespace SCJMapper_V2 { /// /// Provides some items that are user related - packed in one place.. /// class TheUser { private static readonly log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod( ).DeclaringType ); private static bool hasWriteAccessToFolder( string folderPath ) { try { // Attempt to get a list of security permissions from the folder. // This will raise an exception if the path is read only or do not have access to view the permissions. System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl( folderPath ); return true; } catch ( UnauthorizedAccessException ) { return false; } } /// /// Returns the name of the Personal Program folder in My Documents /// Creates the folder if needed /// /// Path to the Personal Program directory static public string UserDir { get { log.Debug( "UserDir - Entry" ); string docPath = Path.Combine( Environment.GetFolderPath( Environment.SpecialFolder.Personal ), Application.ProductName ); if ( !Directory.Exists( docPath ) ) Directory.CreateDirectory( docPath ); return docPath; } } /// /// The directory to store the assets /// static public string FileStoreDir { get { log.Debug( "FileStoreDir - Entry" ); string docPath = AppDir; // fallback if ( !hasWriteAccessToFolder( docPath ) ) docPath = UserDir; return Path.Combine( docPath, "Storage"); } } /// /// The application directory /// static public string AppDir { get => Path.GetDirectoryName( Application.ExecutablePath); } /// /// Returns the mapping file name + path into our user dir /// /// The mapping name /// A fully qualified filename static public string MappingFileName( string mapName ) { log.Debug( "MappingFileName - Entry" ); return Path.Combine( UserDir, mapName + ".xml" ); } /// /// Returns the mapping file name + path into our user dir /// /// The mapping name /// A fully qualified filename static public string MappingCsvFileName( string mapName ) { log.Debug( "MappingCsvFileName - Entry" ); return Path.Combine( UserDir, mapName + ".csv" ); } /// /// Create a backupfile from the given file /// /// The mapping name static public void BackupMappingFile( string mapName ) { log.Debug( "BackupMappingFile - Entry" ); string mf = MappingFileName( mapName ); if ( File.Exists( mf ) ) File.Copy( mf, mf + ".backup", true ); } } }