/// <summary> /// Gets the configuration from the File Cleanup Service /// </summary> private void GetConfiguration() { try { HttpWebResponse webResponse = null; if (SendServiceWebRequest("GetConfiguration", "GET", out webResponse) && webResponse != null && webResponse.StatusCode == HttpStatusCode.OK) { DataContractSerializer serializer = new DataContractSerializer(typeof(FileCleanupConfiguration)); FileCleanupConfiguration config = serializer.ReadObject(webResponse.GetResponseStream()) as FileCleanupConfiguration; foreach (DirectoryConfiguration dir in config.Directories) { dir.IsDirty = false; } config.IsDirty = false; config.PropertyChanged += Configuration_PropertyChanged; DataContext = config; } else { MessageBox.Show("Web response is not OK", "Invalid Response", MessageBoxButton.OK, MessageBoxImage.Error); this.Close(); } } catch (Exception ex) { MessageBox.Show("Exception while getting configurataion: " + Environment.NewLine + ex.ToString(), "Exception", MessageBoxButton.OK, MessageBoxImage.Error); this.Close(); } }
/// <summary> /// Loads the configuration /// </summary> /// <param name="file">Path to configuration file</param> /// <returns>True if successful. False if not.</returns> private bool LoadConfiguration(string file) { try { if (!File.Exists(file)) { _logger.ShowWarning(string.Format("Configuration file does not exist at '{0}'", file)); } else { _config = FileCleanupConfiguration.Load(file); if (_config != null) { _configFile = file; return(true); } } } catch (Exception ex) { _logger.ShowError("Error while loading configuration file."); _logger.ShowError(ex.ToString()); } return(false); }
/// <summary> /// Initializes the service /// </summary> private void Initialize() { Status = Properties.Resources.Status_LoadingConfigurations; try { if (!LoadConfiguration(Properties.Settings.Default.ConfigFile)) { _logger.ShowInformation("Creating new configuration file..."); _config = new FileCleanupConfiguration(); _config.ArchiveDays = 30; _config.ArchiveLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "archive"); _config.CleanupTime = "2200"; _config.Directories.Add(new DirectoryConfiguration() { CleanupAction = CleanupAction.Archive, Path = Path.GetTempPath(), Recursive = true }); _configFile = string.IsNullOrWhiteSpace(Properties.Settings.Default.ConfigFile) ? Path.Combine(Path.GetTempPath(), "fileCleanupConfig.xml") : Properties.Settings.Default.ConfigFile; SaveConfiguration(); } } catch (Exception ex) { _logger.ShowError(ex.ToString()); } }
/// <summary> /// Sets the configuration /// </summary> /// <param name="config">Configuration to set</param> public void SetConfiguration(FileCleanupConfiguration config) { _logger.ShowInformation("Setting new configuration"); lock (_config) { _config = config; SaveConfiguration(); } }