Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Despite the fact that the <code>app.config</code> file can be written to (using <a href="http://msdn.microsoft.com/en-us/library/ms224437.aspx" rel="nofollow noreferrer"><code>ConfigurationManager.OpenExeConfiguration</code></a> to open for writing), the usual practice is to store read-only settings in there.</p> <p>It's easy to write a simple settings class:</p> <pre><code>public sealed class Settings { private readonly string _filename; private readonly XmlDocument _doc = new XmlDocument(); private const string emptyFile = @"&lt;?xml version=""1.0"" encoding=""utf-8"" ?&gt; &lt;configuration&gt; &lt;appSettings&gt; &lt;add key=""defaultkey"" value=""123"" /&gt; &lt;add key=""anotherkey"" value=""abc"" /&gt; &lt;/appSettings&gt; &lt;/configuration&gt;"; public Settings(string path, string filename) { // strip any trailing backslashes... while (path.Length &gt; 0 &amp;&amp; path.EndsWith("\\")) { path = path.Remove(path.Length - 1, 1); } _filename = Path.Combine(path, filename); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (!File.Exists(_filename)) { // Create it... _doc.LoadXml(emptyFile); _doc.Save(_filename); } else { _doc.Load(_filename); } } /// &lt;summary&gt; /// Retrieve a value by name. /// Returns the supplied DefaultValue if not found. /// &lt;/summary&gt; public string Get(string key, string defaultValue) { XmlNode node = _doc.SelectSingleNode("configuration/appSettings/add[@key='" + key + "']"); if (node == null) { return defaultValue; } return node.Attributes["value"].Value ?? defaultValue; } /// &lt;summary&gt; /// Write a config value by key /// &lt;/summary&gt; public void Set(string key, string value) { XmlNode node = _doc.SelectSingleNode("configuration/appSettings/add[@key='" + key + "']"); if (node != null) { node.Attributes["value"].Value = value; _doc.Save(_filename); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload