/// <summary>Loads the settings from the specified file.</summary> private void settingsLoadFromFile() { try { string settingsDir = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), Common.APP_MANUF + "\\" + Common.APP_DIRECTORY); string fileName = Path.Combine(settingsDir, Common.SETTINGS_FILE); if (File.Exists(fileName)) { //Load the XML. XmlSerializer xmlSer = new XmlSerializer(typeof(SettingsStorage_100)); using (FileStream fileStream = new FileStream(fileName, FileMode.Open)) this.settings = (SettingsStorage_100)xmlSer.Deserialize(fileStream); } else { //Need to create a new settings file. The message box was for debugging purposes //MessageBox.Show("Configuration file not found. Creating new file!", "New Installation", MessageBoxButton.OK, MessageBoxImage.Information); //Create a new settings file. this.settings = new SettingsStorage_100(); this.settings.System_Initialized = true; this.WindowSetUnsaved(true); } } catch (Exception ex) { //Show messagebox. MessageBox.Show("There was an error initializing the program:" + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine + "The application will close.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); this.Close(); } }
private const int MILI = 1000 * 60; //Add 60 for seconds. Config file stores value in seconds. /// <summary>Constructor. Creates the thread object, but does not start it.</summary> public PollThread() { const string METHOD = CLASS + ".ctor()"; try { //Create a new log.. _eventLog = new Logger(Common.APP_NAME); //See if our settings file exists, if not create it so that the service can start string inflectraDir = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Inflectra"); string settingsDir = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), Common.APP_MANUF + "\\" + Common.APP_DIRECTORY); if (!Directory.Exists(settingsDir)) { Directory.CreateDirectory(settingsDir); //Update security on the two folders DirectorySecurity dirSecurity = Directory.GetAccessControl(inflectraDir); dirSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); Directory.SetAccessControl(inflectraDir, dirSecurity); dirSecurity = Directory.GetAccessControl(settingsDir); dirSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); Directory.SetAccessControl(settingsDir, dirSecurity); } string fileName = Path.Combine(settingsDir, Common.SETTINGS_FILE); if (!File.Exists(fileName)) { using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { SettingsStorage_100 settings = new SettingsStorage_100(); XmlSerializer ser = new XmlSerializer(settings.GetType()); ser.Serialize(fs, settings); fs.Close(); } //Need to allow all users to modify this file (since the main UI will run as a different user) FileSecurity fileSecurity = File.GetAccessControl(fileName); fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); File.SetAccessControl(fileName, fileSecurity); } //First create an event on our config file. _configWatcher = new FileSystemWatcher(); _configWatcher.Path = settingsDir; _configWatcher.Filter = Common.SETTINGS_FILE; _configWatcher.IncludeSubdirectories = false; _configWatcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.LastWrite | NotifyFilters.Size; _configWatcher.Changed += new FileSystemEventHandler(configFile_Changed); //Read current configuration, first, then start watcher. readSettingsFile(); _configWatcher.EnableRaisingEvents = true; } catch (Exception ex) { //An error occured. Try logging it first, and then rethrow it. try { _eventLog.WriteMessage(METHOD, ex); } catch { } //Rethrow exception. throw; } }