/// <summary> /// Writes a configuration item's value to a file. /// </summary> /// <param name="value">The value to write to the file.</param> /// <param name="log">The event log to log exceptions to. May be null for no logging.</param> /// <returns>True if the write was successful, false otherwise.</returns> protected bool Write(string value, EventLog.EventLog log) { // Write the value to the file. File file = null; try { file = new File(FilePath, false); return(file.WriteLine(value)); } catch (Exception e) { // There was an error opening the file. LogException(e, log); return(false); } finally { // Close the file. if (file != null) { file.Close(); } } }
// ----- METHODS ----- /// <summary> /// Creates a new configuration file that can then be loaded normally. /// Note: This will NOT overwrite an existing file. /// </summary> /// <param name="folderPath">The path of the folder to create the configuration item.</param> /// <param name="name">The name of the configuration item to create.</param> /// <returns>True if the file was created or already exists, false if an error prevented it from being created.</returns> public static bool Create(string folderPath, string name) { string fullPath = folderPath + name + FILE_EXTENSION; if (!File.Exists(fullPath)) { // Create the file. try { // Create and close the file. File file = new File(fullPath, false); file.Close(); return(true); } catch { // There was an error creating the file. return(false); } } else { // The file already exists. return(true); } }
/// <summary> /// Retrieves the value of a configuration item from its file. /// </summary> /// <param name="log">The event log to log exceptions to. May be null for no logging.</param> /// <returns>The value of the configuration item or null if not found.</returns> protected string Get(EventLog.EventLog log) { File file = null; try { // Get the file. file = new File(FilePath, false, readOnly); // Check that the file exists. if (file.Exists()) { // Return the value of the item from the file. return(file.ReadAllAsText()); } else { // The file doesn't exist. Return null. return(null); } } catch (Exception e) { // There was an error opening or reading from the file. LogException(e, log); return(null); } finally { // Close the file. if (file != null) { file.Close(); } } }
/// <summary> /// Writes a configuration item's value to a file. /// </summary> /// <param name="value">The value to write to the file.</param> /// <param name="log">The event log to log exceptions to. May be null for no logging.</param> /// <returns>True if the write was successful, false otherwise.</returns> protected bool Write(string value, EventLog.EventLog log) { // Write the value to the file. File file = null; try { file = new File(FilePath, false); return file.WriteLine(value); } catch (Exception e) { // There was an error opening the file. LogException(e, log); return false; } finally { // Close the file. if (file != null) { file.Close(); } } }
/// <summary> /// Retrieves the value of a configuration item from its file. /// </summary> /// <param name="log">The event log to log exceptions to. May be null for no logging.</param> /// <returns>The value of the configuration item or null if not found.</returns> protected string Get(EventLog.EventLog log) { File file = null; try { // Get the file. file = new File(FilePath, false, readOnly); // Check that the file exists. if (file.Exists()) { // Return the value of the item from the file. return file.ReadAllAsText(); } else { // The file doesn't exist. Return null. return null; } } catch (Exception e) { // There was an error opening or reading from the file. LogException(e, log); return null; } finally { // Close the file. if (file != null) { file.Close(); } } }