Created
March 1, 2017 06:22
-
-
Save edg-l/7a8fe5b7b3cb41b52fc0212d4d18330a to your computer and use it in GitHub Desktop.
a config class c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
namespace Yallr.src.game | |
{ | |
class ConfigFile | |
{ | |
Configuration configManager = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); | |
KeyValueConfigurationCollection confCollection; | |
List<QueryKeyValue> QueriedValues = new List<QueryKeyValue>(); | |
private class QueryKeyValue | |
{ | |
public string Key { get; set; } | |
public string Value { get; set; } | |
public QueryKeyValue(string key, string value) | |
{ | |
Key = key; | |
Value = value; | |
} | |
} | |
public ConfigFile() | |
{ | |
confCollection = configManager.AppSettings.Settings; | |
if (confCollection["player_name"] == null) | |
{ | |
Random rnd = new Random(); | |
confCollection.Add(new KeyValueConfigurationElement("player_name", | |
"Yal#" + rnd.Next(10000, 99999))); | |
} | |
if (confCollection["logfile_name"] == null) | |
confCollection.Add("logfile_name", "yallr_log.txt"); | |
if (confCollection["gfx_screen_width"] == null) | |
confCollection.Add("gfx_screen_width", "1280"); | |
if (confCollection["gfx_screen_height"] == null) | |
confCollection.Add("gfx_screen_height", "720"); | |
if (confCollection["gfx_fullscreen"] == null) | |
confCollection.Add("gfx_fullscreen", "0"); | |
if (confCollection["gfx_borderless"] == null) | |
confCollection.Add("gfx_borderless", "0"); | |
if (confCollection["gfx_vsync"] == null) | |
confCollection.Add("gfx_vsync", "0"); | |
if (confCollection["cl_debug"] == null) | |
confCollection.Add("cl_debug", "0"); | |
configManager.Save(ConfigurationSaveMode.Modified); | |
ConfigurationManager.RefreshSection(configManager.AppSettings.SectionInformation.Name); | |
} | |
/// <summary> | |
/// Gets the value of a setting | |
/// </summary> | |
/// <param name="key">The setting to get</param> | |
/// <returns>The setting value</returns> | |
public string Get(Constants.ConfigKeys key) | |
{ | |
return confCollection[key.ToString()].Value; | |
} | |
/// <summary> | |
/// Sets a setting by a given key to the value and saves it. | |
/// </summary> | |
/// <param name="key">The setting to change</param> | |
/// <param name="value">The value to assign</param> | |
public void Set(Constants.ConfigKeys key, string value) | |
{ | |
confCollection[key.ToString()].Value = value; | |
configManager.Save(ConfigurationSaveMode.Modified); | |
ConfigurationManager.RefreshSection(configManager.AppSettings.SectionInformation.Name); | |
} | |
/// <summary> | |
/// Queries a setting to be saved later | |
/// </summary> | |
/// <param name="key">The setting to change</param> | |
/// <param name="value">The value to assign</param> | |
public void QuerySave(Constants.ConfigKeys key, string value) | |
{ | |
QueriedValues.Add(new QueryKeyValue(key.ToString(), value)); | |
} | |
/// <summary> | |
/// Save all the settings in the current query, and cleans it. | |
/// </summary> | |
public void SaveQ() | |
{ | |
foreach(var k in QueriedValues) | |
{ | |
confCollection[k.Key].Value = k.Value; | |
} | |
configManager.Save(ConfigurationSaveMode.Modified); | |
ConfigurationManager.RefreshSection(configManager.AppSettings.SectionInformation.Name); | |
QueriedValues.Clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment