PeanutButter
Loading...
Searching...
No Matches
PeanutButter.INI.INIFile Class Reference

More...

Inheritance diagram for PeanutButter.INI.INIFile:
PeanutButter.INI.IINIFile

Public Member Functions

 INIFile ()
 Constructs an instance of INIFile without parsing any files, defaulting to best effort parser.
 
 INIFile (string path)
 Constructs an instance of INIFile, parsing the provided path if found, with the best-effort parser.
 
 INIFile (ILineParser lineParser)
 Constructs an instance of INIFile with a custom line parser.
 
 INIFile (string path, ILineParser customLineParser)
 Constructs an instance of INIFile with a custom line parser.
 
 INIFile (string path, ParseStrategies parseStrategy)
 Constructs an instance of INIFile, parsing the provided path if found, with escaped characters enabled if enableEscapeCharacters is true.
 
void Load (string path)
 Attempts to load the file at the given path, discarding any existing config.
Parameters
path

 
void Load (string path, Encoding encoding)
 Attempts to load the file at the given path, discarding any existing config.
Parameters
path
encoding

 
void Load (string path, ParseStrategies parseStrategy)
 Attempts to load the file at the given path, discarding any existing config.
Parameters
path
parseStrategy

 
void Load (string path, ParseStrategies parseStrategy, Encoding encoding)
 Attempts to load the file at the given path, discarding any existing config.
Parameters
path
parseStrategy
encoding

 
void Parse (string contents)
 Parses the string contents, loading as ini config.
Will remove any prior config.
Parameters
contents

 
IDictionary< string, string > GetSection (string section)
 Retrieve the collection of settings for a section by section name.
Parameters
name
Returns

 
void AddSection (string section, params string[] comments)
 Add a section by name.
Parameters
sectionName of the section to add
comments(Optional) comments for the section

 
void RemoveSection (string section)
 Remove a section by name.
Parameters
section

 
void RenameSection (string existingName, string newName)
 Rename a section.
Parameters
existingNameName of the existing section
newNameNew name of the section that is to be renamed

 
void Merge (string iniPath, MergeStrategies mergeStrategy)
 Merges in the ini config from the provided file path.
  • ignores paths which don't exist
Parameters
iniPathFile to merge in to current config
mergeStrategyStrategy to pick when merging

 
void Merge (IINIFile other, MergeStrategies mergeStrategy)
 Merges another ini file into this one.
Parameters
otherother ini file
mergeStrategystrategy to use when encountering conflicts

 
void Reload ()
 Reload config (and all merged config) from disk.
 
void Persist ()
 Persists to the last-loaded file, excluding merged configuration, with the default encoding.
 
void Persist (Encoding encoding)
 Persists to the last-loaded file with the provided encoding.
Parameters
encoding

 
void Persist (PersistStrategies persistStrategy)
 Persists to the last-loaded file with the specified merge strategy and default encoding.
Parameters
persistStrategyStrategy to employ for merged config

 
void Persist (PersistStrategies persistStrategy, Encoding encoding)
 Persists to the last-loaded file with the specified merge strategy and encoding.
Parameters
persistStrategyStrategy to employ for merged config
encodingThe encoding to use for the written file

 
void Persist (string saveToPath)
 Persists to the specified path, excluding merged configuration, with the default encoding.
Parameters
saveToPath

 
void Persist (string saveToPath, Encoding encoding)
 Persists to the specified path, excluding merged configuration, with the specified encoding.
Parameters
saveToPath
encodingEncoding to use

 
void Persist (string saveToPath, PersistStrategies persistStrategy)
 Persists to the specified path with the specified strategy and default encoding.
Parameters
saveToPathFile to save to
persistStrategyStrategy to employ for merged config

 
void Persist (string saveToPath, PersistStrategies persistStrategy, Encoding encoding)
 Persists to the specified path with the specified strategy and the specified encoding.
Parameters
saveToPathFile to save to
persistStrategyStrategy to employ for merged config
encodingEncoding to use when writing the file

 
void Persist (Stream toStream)
 Persists to the specified stream, excluding merged configuration, with the default encoding

Parameters
toStreamStream to persist to

 
void Persist (Stream toStream, Encoding encoding)
 Persists to the specified stream, excluding merged configuration, with the provided encoding

Parameters
toStreamStream to persist to
encodingEncoding to use

 
void Persist (Stream toStream, PersistStrategies persistStrategy)
 Persists to the specified stream, excluding merged configuration

Parameters
toStreamStream to persist to
persistStrategyStrategy to employ for merged config

 
void Persist (Stream toStream, PersistStrategies persistStrategy, Encoding encoding)
 Persists to the specified stream, using the provided persistence strategy and the provided encoding

Parameters
toStreamStream to persist to
persistStrategyStrategy to employ for merged config
encodingEncoding to use

 
override string ToString ()
 
void SetValue (string section, string key, string value)
 Sets a value by section and key.
Parameters
section
key
value

 
bool RemoveValue (string section, string key)
 Removes a value from the section.
  • will not remove empty sections
  • this is functionally equivalent to ini["section"].Remove("key")
Parameters
sectionSection to remove the setting from
keySetting to remove
Returns

 
bool RemoveValue (string section, string key, bool removeEmptySection)
 Removes a value from the section.
  • optionally removes empty sections
Parameters
sectionSection to remove the setting from
keySetting to remove
removeEmptySectionWhether or not to clear out empty sections
Returns

 
string GetValue (string section, string key, string defaultValue=null)
 Get a configured value by section and key, optionally provide a fallback default value.
Parameters
section
key
defaultValue
Returns

 
bool HasSection (string section)
 Test if a section exists by name.
Parameters
section
Returns

 
bool HasSetting (string section, string key)
 Test if a setting exists by section and key.
Parameters
section
key
Returns

 

Static Public Member Functions

static INIFile FromString (string ini)
 Loads INI data from a string.
 

Properties

ParseStrategies ParseStrategy = ParseStrategies.BestEffort [get, set]
 Whether or not to handle escape characters in ini values When enabled (default), then the following sequences in values are supported: \ -> backslash " -> quote.
 
ILineParser CustomLineParser [get, set]
 Provide a custom line parser if you like.
 
string SectionSeparator = "" [get, set]
 Separate sections with any string you like.
  • defaults to empty, which inserts a new line
  • set to null for no separator at all

 
bool AppendTrailingNewLine = false [get, set]
 Toggle whether a new line is added at the bottom when persisted.
 
bool WrapValueInQuotes = true [get, set]
 Toggle whether key values are wrapped in quote marks when persisted.
 
IEnumerable< string > Sections [get]
 List all the currently-available sections.
 
IEnumerable< string > MergedSections [get]
 Provides an enumeration over all merged section names.
 
IEnumerable< string > AllSections [get]
 Provides an enumeration over all section names: whether from merging or the initial load.
 
string Path [get]
 Exposes the path of the loaded INIFile.
 
Dictionary< string, IDictionary< string, string > > Data [get]
 Data storage for the current INI data.
 
Encoding DefaultEncoding [get, set]
 The default encoding to use when persisting files. You may specify an encoding at persistence time too.
 
IDictionary< string, string > this[string index] [get, set]
 Interface to treat IINIFile like a dictionary of dictionaries.
Parameters
index
Returns

 
- Properties inherited from PeanutButter.INI.IINIFile

Detailed Description

Constructor & Destructor Documentation

◆ INIFile() [1/4]

PeanutButter.INI.INIFile.INIFile ( string  path)

Constructs an instance of INIFile, parsing the provided path if found, with the best-effort parser.

Parameters
pathPath to an existing ini file. Will not error if not found, but will be used as the default path to persist to.

◆ INIFile() [2/4]

PeanutButter.INI.INIFile.INIFile ( ILineParser  lineParser)

Constructs an instance of INIFile with a custom line parser.

Parameters
lineParser

◆ INIFile() [3/4]

PeanutButter.INI.INIFile.INIFile ( string  path,
ILineParser  customLineParser 
)

Constructs an instance of INIFile with a custom line parser.

Parameters
path
customLineParser

◆ INIFile() [4/4]

PeanutButter.INI.INIFile.INIFile ( string  path,
ParseStrategies  parseStrategy 
)

Constructs an instance of INIFile, parsing the provided path if found, with escaped characters enabled if enableEscapeCharacters is true.

Parameters
pathPath to an existing ini file. Will not error if not found, but will be used as the default path to persist to.
parseStrategy

Member Function Documentation

◆ AddSection()

void PeanutButter.INI.INIFile.AddSection ( string  section,
params string[]  comments 
)

Add a section by name.

Parameters
sectionName of the section to add
comments(Optional) comments for the section

Implements PeanutButter.INI.IINIFile.

◆ FromString()

static INIFile PeanutButter.INI.INIFile.FromString ( string  ini)
static

Loads INI data from a string.

Parameters
ini
Returns

◆ GetSection()

IDictionary< string, string > PeanutButter.INI.INIFile.GetSection ( string  section)

Retrieve the collection of settings for a section by section name.

Parameters
name
Returns

Implements PeanutButter.INI.IINIFile.

◆ GetValue()

string PeanutButter.INI.INIFile.GetValue ( string  section,
string  key,
string  defaultValue = null 
)

Get a configured value by section and key, optionally provide a fallback default value.

Parameters
section
key
defaultValue
Returns

Implements PeanutButter.INI.IINIFile.

◆ HasSection()

bool PeanutButter.INI.INIFile.HasSection ( string  section)

Test if a section exists by name.

Parameters
section
Returns

Implements PeanutButter.INI.IINIFile.

◆ HasSetting()

bool PeanutButter.INI.INIFile.HasSetting ( string  section,
string  key 
)

Test if a setting exists by section and key.

Parameters
section
key
Returns

Implements PeanutButter.INI.IINIFile.

◆ Load() [1/4]

void PeanutButter.INI.INIFile.Load ( string  path)

Attempts to load the file at the given path, discarding any existing config.

Parameters
path

Implements PeanutButter.INI.IINIFile.

◆ Load() [2/4]

void PeanutButter.INI.INIFile.Load ( string  path,
Encoding  encoding 
)

Attempts to load the file at the given path, discarding any existing config.

Parameters
path
encoding

Implements PeanutButter.INI.IINIFile.

◆ Load() [3/4]

void PeanutButter.INI.INIFile.Load ( string  path,
ParseStrategies  parseStrategy 
)

Attempts to load the file at the given path, discarding any existing config.

Parameters
path
parseStrategy

Implements PeanutButter.INI.IINIFile.

◆ Load() [4/4]

void PeanutButter.INI.INIFile.Load ( string  path,
ParseStrategies  parseStrategy,
Encoding  encoding 
)

Attempts to load the file at the given path, discarding any existing config.

Parameters
path
parseStrategy
encoding

Implements PeanutButter.INI.IINIFile.

◆ Merge() [1/2]

void PeanutButter.INI.INIFile.Merge ( IINIFile  other,
MergeStrategies  mergeStrategy 
)

Merges another ini file into this one.

Parameters
otherother ini file
mergeStrategystrategy to use when encountering conflicts

Implements PeanutButter.INI.IINIFile.

◆ Merge() [2/2]

void PeanutButter.INI.INIFile.Merge ( string  iniPath,
MergeStrategies  mergeStrategy 
)

Merges in the ini config from the provided file path.

  • ignores paths which don't exist
Parameters
iniPathFile to merge in to current config
mergeStrategyStrategy to pick when merging

Implements PeanutButter.INI.IINIFile.

◆ Parse()

void PeanutButter.INI.INIFile.Parse ( string  contents)

Parses the string contents, loading as ini config.
Will remove any prior config.

Parameters
contents

Implements PeanutButter.INI.IINIFile.

◆ Persist() [1/12]

void PeanutButter.INI.INIFile.Persist ( )

Persists to the last-loaded file, excluding merged configuration, with the default encoding.

Implements PeanutButter.INI.IINIFile.

◆ Persist() [2/12]

void PeanutButter.INI.INIFile.Persist ( Encoding  encoding)

Persists to the last-loaded file with the provided encoding.

Parameters
encoding

Implements PeanutButter.INI.IINIFile.

◆ Persist() [3/12]

void PeanutButter.INI.INIFile.Persist ( PersistStrategies  persistStrategy)

Persists to the last-loaded file with the specified merge strategy and default encoding.

Parameters
persistStrategyStrategy to employ for merged config

Implements PeanutButter.INI.IINIFile.

◆ Persist() [4/12]

void PeanutButter.INI.INIFile.Persist ( PersistStrategies  persistStrategy,
Encoding  encoding 
)

Persists to the last-loaded file with the specified merge strategy and encoding.

Parameters
persistStrategyStrategy to employ for merged config
encodingThe encoding to use for the written file

Implements PeanutButter.INI.IINIFile.

◆ Persist() [5/12]

void PeanutButter.INI.INIFile.Persist ( Stream  toStream)

Persists to the specified stream, excluding merged configuration, with the default encoding

Parameters
toStreamStream to persist to

Implements PeanutButter.INI.IINIFile.

◆ Persist() [6/12]

void PeanutButter.INI.INIFile.Persist ( Stream  toStream,
Encoding  encoding 
)

Persists to the specified stream, excluding merged configuration, with the provided encoding

Parameters
toStreamStream to persist to
encodingEncoding to use

Implements PeanutButter.INI.IINIFile.

◆ Persist() [7/12]

void PeanutButter.INI.INIFile.Persist ( Stream  toStream,
PersistStrategies  persistStrategy 
)

Persists to the specified stream, excluding merged configuration

Parameters
toStreamStream to persist to
persistStrategyStrategy to employ for merged config

Implements PeanutButter.INI.IINIFile.

◆ Persist() [8/12]

void PeanutButter.INI.INIFile.Persist ( Stream  toStream,
PersistStrategies  persistStrategy,
Encoding  encoding 
)

Persists to the specified stream, using the provided persistence strategy and the provided encoding

Parameters
toStreamStream to persist to
persistStrategyStrategy to employ for merged config
encodingEncoding to use

Implements PeanutButter.INI.IINIFile.

◆ Persist() [9/12]

void PeanutButter.INI.INIFile.Persist ( string  saveToPath)

Persists to the specified path, excluding merged configuration, with the default encoding.

Parameters
saveToPath

Implements PeanutButter.INI.IINIFile.

◆ Persist() [10/12]

void PeanutButter.INI.INIFile.Persist ( string  saveToPath,
Encoding  encoding 
)

Persists to the specified path, excluding merged configuration, with the specified encoding.

Parameters
saveToPath
encodingEncoding to use

Implements PeanutButter.INI.IINIFile.

◆ Persist() [11/12]

void PeanutButter.INI.INIFile.Persist ( string  saveToPath,
PersistStrategies  persistStrategy 
)

Persists to the specified path with the specified strategy and default encoding.

Parameters
saveToPathFile to save to
persistStrategyStrategy to employ for merged config

Implements PeanutButter.INI.IINIFile.

◆ Persist() [12/12]

void PeanutButter.INI.INIFile.Persist ( string  saveToPath,
PersistStrategies  persistStrategy,
Encoding  encoding 
)

Persists to the specified path with the specified strategy and the specified encoding.

Parameters
saveToPathFile to save to
persistStrategyStrategy to employ for merged config
encodingEncoding to use when writing the file

Implements PeanutButter.INI.IINIFile.

◆ Reload()

void PeanutButter.INI.INIFile.Reload ( )

Reload config (and all merged config) from disk.

Implements PeanutButter.INI.IINIFile.

◆ RemoveSection()

void PeanutButter.INI.INIFile.RemoveSection ( string  section)

Remove a section by name.

Parameters
section

Implements PeanutButter.INI.IINIFile.

◆ RemoveValue() [1/2]

bool PeanutButter.INI.INIFile.RemoveValue ( string  section,
string  key 
)

Removes a value from the section.

  • will not remove empty sections
  • this is functionally equivalent to ini["section"].Remove("key")
Parameters
sectionSection to remove the setting from
keySetting to remove
Returns

Implements PeanutButter.INI.IINIFile.

◆ RemoveValue() [2/2]

bool PeanutButter.INI.INIFile.RemoveValue ( string  section,
string  key,
bool  removeEmptySection 
)

Removes a value from the section.

  • optionally removes empty sections
Parameters
sectionSection to remove the setting from
keySetting to remove
removeEmptySectionWhether or not to clear out empty sections
Returns

Implements PeanutButter.INI.IINIFile.

◆ RenameSection()

void PeanutButter.INI.INIFile.RenameSection ( string  existingName,
string  newName 
)

Rename a section.

Parameters
existingNameName of the existing section
newNameNew name of the section that is to be renamed

Implements PeanutButter.INI.IINIFile.

◆ SetValue()

void PeanutButter.INI.INIFile.SetValue ( string  section,
string  key,
string  value 
)

Sets a value by section and key.

Parameters
section
key
value

Implements PeanutButter.INI.IINIFile.

Property Documentation

◆ AllSections

IEnumerable<string> PeanutButter.INI.INIFile.AllSections
get

Provides an enumeration over all section names: whether from merging or the initial load.

Implements PeanutButter.INI.IINIFile.

◆ AppendTrailingNewLine

bool PeanutButter.INI.INIFile.AppendTrailingNewLine = false
getset

Toggle whether a new line is added at the bottom when persisted.

Implements PeanutButter.INI.IINIFile.

◆ CustomLineParser

ILineParser PeanutButter.INI.INIFile.CustomLineParser
getset

Provide a custom line parser if you like.

Implements PeanutButter.INI.IINIFile.

◆ Data

Dictionary<string, IDictionary<string, string> > PeanutButter.INI.INIFile.Data
getprotected
Initial value:
=
CreateCaseInsensitiveDictionary()

Data storage for the current INI data.

◆ DefaultEncoding

Encoding PeanutButter.INI.INIFile.DefaultEncoding
getset

The default encoding to use when persisting files. You may specify an encoding at persistence time too.

Implements PeanutButter.INI.IINIFile.

◆ MergedSections

IEnumerable<string> PeanutButter.INI.INIFile.MergedSections
get

Provides an enumeration over all merged section names.

Implements PeanutButter.INI.IINIFile.

◆ ParseStrategy

ParseStrategies PeanutButter.INI.INIFile.ParseStrategy = ParseStrategies.BestEffort
getset

Whether or not to handle escape characters in ini values When enabled (default), then the following sequences in values are supported: \ -> backslash " -> quote.

Implements PeanutButter.INI.IINIFile.

◆ Path

string PeanutButter.INI.INIFile.Path
get

Exposes the path of the loaded INIFile.

Implements PeanutButter.INI.IINIFile.

◆ Sections

IEnumerable<string> PeanutButter.INI.INIFile.Sections
get

List all the currently-available sections.

Implements PeanutButter.INI.IINIFile.

◆ SectionSeparator

string PeanutButter.INI.INIFile.SectionSeparator = ""
getset

Separate sections with any string you like.

  • defaults to empty, which inserts a new line
  • set to null for no separator at all

Implements PeanutButter.INI.IINIFile.

◆ this[string index]

IDictionary<string, string> PeanutButter.INI.INIFile.this[string index]
getset

Interface to treat IINIFile like a dictionary of dictionaries.

Parameters
index
Returns

Implements PeanutButter.INI.IINIFile.

◆ WrapValueInQuotes

bool PeanutButter.INI.INIFile.WrapValueInQuotes = true
getset

Toggle whether key values are wrapped in quote marks when persisted.

Implements PeanutButter.INI.IINIFile.


The documentation for this class was generated from the following file: