Пример #1
0
        // ----- 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);
            }
        }
Пример #2
0
 /// <summary>
 /// Deletes the configuration item.
 /// </summary>
 /// <returns>True if the item was deleted, false otherwise.</returns>
 public bool Delete()
 {
     // Check if the file exists.
     if (File.Exists(FilePath))
     {
         // The file exists.
         // Delete the file.
         return(File.Delete(FilePath));
     }
     else
     {
         // The file doesn't exist.
         return(true);
     }
 }
Пример #3
0
        /// <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();
                }
            }
        }
Пример #4
0
        /// <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();
                }
            }
        }