Skip to content

ConfigFile Class

A helper class to handle persistent data. All public methods are thread-safe.

C#
public class ConfigFile : IDictionary<ConfigDefinition, ConfigEntryBase>, ICollection<KeyValuePair<ConfigDefinition, ConfigEntryBase>>, IEnumerable<KeyValuePair<ConfigDefinition, ConfigEntryBase>>, IEnumerable

Inheritance object

Implements IDictionary<ConfigDefinition, ConfigEntryBase>, ICollection<KeyValuePair<ConfigDefinition, ConfigEntryBase>>, IEnumerable<KeyValuePair<ConfigDefinition, ConfigEntryBase>>, IEnumerable

Create a new config file at the specified config path.

C#
public ConfigFile(string configPath, bool saveOnInit)

configPath string
Full path to a file that contains settings. The file will be created as needed.

saveOnInit bool
If the config file/directory doesn’t exist, create it immediately.

Create a new config file at the specified config path.

C#
public ConfigFile(string configPath, bool saveOnInit, BepInPlugin ownerMetadata)

configPath string
Full path to a file that contains settings. The file will be created as needed.

saveOnInit bool
If the config file/directory doesn’t exist, create it immediately.

ownerMetadata BepInPlugin
Information about the plugin that owns this setting file.

C#
public static ConfigFile CoreConfig { get; }

All config entries inside

C#
protected Dictionary<ConfigDefinition, ConfigEntryBase> Entries { get; }

Create a list with all config entries inside of this config file.

C#
[Obsolete("Use Keys instead")]
public ReadOnlyCollection<ConfigDefinition> ConfigDefinitions { get; }

Full path to the config file. The file might not exist until a setting is added and changed, or Save() is called.

C#
public string ConfigFilePath { get; }

If enabled, writes the config to disk every time a value is set. If disabled, you have to manually use Save() or the changes will be lost!

C#
public bool SaveOnConfigSet { get; set; }
C#
public ConfigEntryBase this[ConfigDefinition key] { get; }
C#
public ConfigEntryBase this[string section, string key] { get; }

Gets the number of elements contained in the ICollection<T>.

C#
public int Count { get; }

Gets a value indicating whether the ICollection<T> is read-only.

C#
public bool IsReadOnly { get; }

Returns the ConfigDefinitions that the ConfigFile contains.

Creates a new array when the property is accessed. Thread-safe.

C#
public ICollection<ConfigDefinition> Keys { get; }

Returns the ConfigEntryBase values that the ConfigFile contains.

Creates a new array when the property is accessed. Thread-safe.

C#
public ICollection<ConfigEntryBase> Values { get; }

Generate user-readable comments for each of the settings in the saved .cfg file.

C#
public bool GenerateSettingDescriptions { get; set; }

Returns an enumerator that iterates through the collection.

C#
public IEnumerator<KeyValuePair<ConfigDefinition, ConfigEntryBase>> GetEnumerator()

IEnumerator<KeyValuePair<ConfigDefinition, ConfigEntryBase>>
An enumerator that can be used to iterate through the collection.

Contains(KeyValuePair<ConfigDefinition, ConfigEntryBase>)

Section titled “Contains(KeyValuePair<ConfigDefinition, ConfigEntryBase>)”

Determines whether the ICollection<T> contains a specific value.

C#
public bool Contains(KeyValuePair<ConfigDefinition, ConfigEntryBase> item)

item KeyValuePair<ConfigDefinition, ConfigEntryBase>
The object to locate in the .

bool
true if item is found in the ICollection<T>; otherwise, false.

Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.

C#
public bool ContainsKey(ConfigDefinition key)

key ConfigDefinition
The key to locate in the .

bool
true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.

ArgumentNullException
key is null.

Adds an element with the provided key and value to the IDictionary<TKey, TValue>.

C#
public void Add(ConfigDefinition key, ConfigEntryBase value)

key ConfigDefinition
The object to use as the key of the element to add.

value ConfigEntryBase
The object to use as the value of the element to add.

ArgumentNullException
key is null.

ArgumentException
An element with the same key already exists in the IDictionary<TKey, TValue>.

NotSupportedException
The IDictionary<TKey, TValue> is read-only.

Removes the element with the specified key from the IDictionary<TKey, TValue>.

C#
public bool Remove(ConfigDefinition key)

key ConfigDefinition
The key of the element to remove.

bool
true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original IDictionary<TKey, TValue>.

ArgumentNullException
key is null.

NotSupportedException
The IDictionary<TKey, TValue> is read-only.

Removes all items from the ICollection<T>.

C#
public void Clear()

NotSupportedException
The ICollection<T> is read-only.

Create an array with all config entries inside of this config file. Should be only used for metadata purposes. If you want to access and modify an existing setting then use BepInEx.Configuration.ConfigFile.AddSetting``1(BepInEx.Configuration.ConfigDefinition%2c``0%2cBepInEx.Configuration.ConfigDescription) instead with no description.

C#
[Obsolete("Use Values instead")]
public ConfigEntryBase[] GetConfigEntries()

ConfigEntryBase[]

Reloads the config from disk. Unsaved changes are lost.

C#
public void Reload()

Writes the config to disk.

C#
public void Save()

Access one of the existing settings. If the setting has not been added yet, null is returned. If the setting exists but has a different type than T, an exception is thrown. New settings should be added with BepInEx.Configuration.ConfigFile.AddSetting``1(BepInEx.Configuration.ConfigDefinition%2c``0%2cBepInEx.Configuration.ConfigDescription).

C#
[Obsolete("Use ConfigFile[key] or TryGetEntry instead")]
public ConfigEntry<T> GetSetting<T>(ConfigDefinition configDefinition)

configDefinition ConfigDefinition
Section and Key of the setting.

ConfigEntry<T>

Access one of the existing settings. If the setting has not been added yet, null is returned. If the setting exists but has a different type than T, an exception is thrown. New settings should be added with BepInEx.Configuration.ConfigFile.AddSetting``1(BepInEx.Configuration.ConfigDefinition%2c``0%2cBepInEx.Configuration.ConfigDescription).

C#
[Obsolete("Use ConfigFile[key] or TryGetEntry instead")]
public ConfigEntry<T> GetSetting<T>(string section, string key)

section string
Section/category/group of the setting. Settings are grouped by this.

key string
Name of the setting.

ConfigEntry<T>

TryGetEntry(ConfigDefinition, out ConfigEntry)

Section titled “TryGetEntry(ConfigDefinition, out ConfigEntry)”

Access one of the existing settings. If the setting has not been added yet, false is returned. Otherwise, true. If the setting exists but has a different type than T, an exception is thrown. New settings should be added with BepInEx.Configuration.ConfigFile.Bind``1(BepInEx.Configuration.ConfigDefinition%2c``0%2cBepInEx.Configuration.ConfigDescription).

C#
public bool TryGetEntry<T>(ConfigDefinition configDefinition, out ConfigEntry<T> entry)

configDefinition ConfigDefinition
Section and Key of the setting.

entry ConfigEntry<T>
The ConfigEntry value to return.

bool

TryGetEntry(string, string, out ConfigEntry)

Section titled “TryGetEntry(string, string, out ConfigEntry)”

Access one of the existing settings. If the setting has not been added yet, null is returned. If the setting exists but has a different type than T, an exception is thrown. New settings should be added with BepInEx.Configuration.ConfigFile.Bind``1(BepInEx.Configuration.ConfigDefinition%2c``0%2cBepInEx.Configuration.ConfigDescription).

C#
public bool TryGetEntry<T>(string section, string key, out ConfigEntry<T> entry)

section string
Section/category/group of the setting. Settings are grouped by this.

key string
Name of the setting.

entry ConfigEntry<T>
The ConfigEntry value to return.

bool

Bind(ConfigDefinition, T, ConfigDescription)

Section titled “Bind(ConfigDefinition, T, ConfigDescription)”

Create a new setting. The setting is saved to drive and loaded automatically. Each definition can be used to add only one setting, trying to add a second setting will throw an exception.

C#
public ConfigEntry<T> Bind<T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)

configDefinition ConfigDefinition
Section and Key of the setting.

defaultValue T
Value of the setting if the setting was not created yet.

configDescription ConfigDescription
Description of the setting shown to the user and other metadata.

ConfigEntry<T>

Bind(string, string, T, ConfigDescription)

Section titled “Bind(string, string, T, ConfigDescription)”

Create a new setting. The setting is saved to drive and loaded automatically. Each section and key pair can be used to add only one setting, trying to add a second setting will throw an exception.

C#
public ConfigEntry<T> Bind<T>(string section, string key, T defaultValue, ConfigDescription configDescription = null)

section string
Section/category/group of the setting. Settings are grouped by this.

key string
Name of the setting.

defaultValue T
Value of the setting if the setting was not created yet.

configDescription ConfigDescription
Description of the setting shown to the user and other metadata.

ConfigEntry<T>

Create a new setting. The setting is saved to drive and loaded automatically. Each section and key pair can be used to add only one setting, trying to add a second setting will throw an exception.

C#
public ConfigEntry<T> Bind<T>(string section, string key, T defaultValue, string description)

section string
Section/category/group of the setting. Settings are grouped by this.

key string
Name of the setting.

defaultValue T
Value of the setting if the setting was not created yet.

description string
Simple description of the setting shown to the user.

ConfigEntry<T>

AddSetting(ConfigDefinition, T, ConfigDescription)

Section titled “AddSetting(ConfigDefinition, T, ConfigDescription)”

Create a new setting. The setting is saved to drive and loaded automatically. Each definition can be used to add only one setting, trying to add a second setting will throw an exception.

C#
[Obsolete("Use Bind instead")]
public ConfigEntry<T> AddSetting<T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)

configDefinition ConfigDefinition
Section and Key of the setting.

defaultValue T
Value of the setting if the setting was not created yet.

configDescription ConfigDescription
Description of the setting shown to the user and other metadata.

ConfigEntry<T>

AddSetting(string, string, T, ConfigDescription)

Section titled “AddSetting(string, string, T, ConfigDescription)”

Create a new setting. The setting is saved to drive and loaded automatically. Each section and key pair can be used to add only one setting, trying to add a second setting will throw an exception.

C#
[Obsolete("Use Bind instead")]
public ConfigEntry<T> AddSetting<T>(string section, string key, T defaultValue, ConfigDescription configDescription = null)

section string
Section/category/group of the setting. Settings are grouped by this.

key string
Name of the setting.

defaultValue T
Value of the setting if the setting was not created yet.

configDescription ConfigDescription
Description of the setting shown to the user and other metadata.

ConfigEntry<T>

Create a new setting. The setting is saved to drive and loaded automatically. Each section and key pair can be used to add only one setting, trying to add a second setting will throw an exception.

C#
[Obsolete("Use Bind instead")]
public ConfigEntry<T> AddSetting<T>(string section, string key, T defaultValue, string description)

section string
Section/category/group of the setting. Settings are grouped by this.

key string
Name of the setting.

defaultValue T
Value of the setting if the setting was not created yet.

description string
Simple description of the setting shown to the user.

ConfigEntry<T>

Access a setting. Use Bind instead.

C#
[Obsolete("Use Bind instead")]
public ConfigWrapper<T> Wrap<T>(string section, string key, string description = null, T defaultValue = default)

section string

key string

description string

defaultValue T

ConfigWrapper<T>

Access a setting. Use Bind instead.

C#
[Obsolete("Use Bind instead")]
public ConfigWrapper<T> Wrap<T>(ConfigDefinition configDefinition, T defaultValue = default)

configDefinition ConfigDefinition

defaultValue T

ConfigWrapper<T>

An event that is fired every time the config is reloaded.

C#
public event EventHandler ConfigReloaded

EventHandler

Fired when one of the settings is changed.

C#
public event EventHandler<SettingChangedEventArgs> SettingChanged

EventHandler<SettingChangedEventArgs>