// Static Methods /// <summary> /// Loads the <see cref="AdoSecurityCache"/> for the current local user. /// </summary> /// <returns>Loaded instance of the <see cref="AdoSecurityCache"/>.</returns> public static AdoSecurityCache GetCurrentCache() { AdoSecurityCache currentCache; AdoSecurityCache localSecurityCache = null; // Define default cache path string cachePath = null; try { // Attempt to retrieve configuration cache path as defined in the config file ConfigurationFile configFile = ConfigurationFile.Current; CategorizedSettingsElementCollection systemSettings = configFile.Settings["systemSettings"]; CategorizedSettingsElement configurationCachePathSetting = systemSettings["ConfigurationCachePath"]; if ((object)configurationCachePathSetting != null) cachePath = FilePath.GetAbsolutePath(systemSettings["ConfigurationCachePath"].Value); if (string.IsNullOrEmpty(cachePath)) cachePath = string.Format("{0}{1}ConfigurationCache{1}", FilePath.GetAbsolutePath(""), Path.DirectorySeparatorChar); } catch (ConfigurationErrorsException) { cachePath = string.Format("{0}{1}ConfigurationCache{1}", FilePath.GetAbsolutePath(""), Path.DirectorySeparatorChar); } string localCacheFileName = Path.Combine(cachePath, DefaultCacheFileName); try { // Make sure configuration cache path exists if (!Directory.Exists(cachePath)) Directory.CreateDirectory(cachePath); // Initialize local ADO security cache (application may only have read-only access to this cache) localSecurityCache = new AdoSecurityCache { FileName = localCacheFileName, ReloadOnChange = false, AutoSave = false }; // Load initial ADO security data set localSecurityCache.Load(); // Validate that current user has write access to the local cache folder string tempFile = FilePath.GetDirectoryName(localCacheFileName) + Guid.NewGuid() + ".tmp"; using (File.Create(tempFile)) { } if (File.Exists(tempFile)) File.Delete(tempFile); // No access issues exist, use local cache as the primary cache currentCache = localSecurityCache; currentCache.AutoSave = true; localSecurityCache = null; } catch (UnauthorizedAccessException) { // User does not have needed serialization access to common cache folder, // use a path where user will have rights string userCacheFolder = FilePath.AddPathSuffix(FilePath.GetApplicationDataFolder()); string userCacheFileName = userCacheFolder + FilePath.GetFileName(localCacheFileName); // Make sure user directory exists if (!Directory.Exists(userCacheFolder)) Directory.CreateDirectory(userCacheFolder); // Copy existing common cache if none exists if (File.Exists(localCacheFileName) && !File.Exists(userCacheFileName)) File.Copy(localCacheFileName, userCacheFileName); // Initialize primary cache within user folder currentCache = new AdoSecurityCache { FileName = userCacheFileName, ReloadOnChange = false, AutoSave = true }; // Load initial ADO security data set currentCache.Load(); // Update user located security cache if locally located security cache is newer if ((object)localSecurityCache != null && File.Exists(localCacheFileName) && File.Exists(userCacheFileName) && File.GetLastWriteTime(localCacheFileName) > File.GetLastWriteTime(userCacheFileName)) currentCache.DataSet = localSecurityCache.DataSet; } if ((object)localSecurityCache != null) localSecurityCache.Dispose(); return currentCache; }