<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kylir&#039;s Website &#187; C#</title>
	<atom:link href="http://www.kylirhorton.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kylirhorton.com</link>
	<description></description>
	<lastBuildDate>Mon, 19 Jul 2010 15:24:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to read and write settings in App.config with C#</title>
		<link>http://www.kylirhorton.com/2008/read-and-write-settings-in-appconfig/</link>
		<comments>http://www.kylirhorton.com/2008/read-and-write-settings-in-appconfig/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 21:13:29 +0000</pubDate>
		<dc:creator>Kylir</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.kylirhorton.com/?p=59</guid>
		<description><![CDATA[Today I&#8217;ve been working on creating a simple WPF application. As a part of this application, I want to store some user-defined preferences. This post briefly details how I went about doing this using C# and Visual Studio 2008. First, make sure your application has an App.config file. If it is missing, add it by [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve been working on creating a simple WPF application. As a part of this application, I want to store some user-defined preferences. This post briefly details how I went about doing this using C# and Visual Studio 2008.</p>
<p>First, make sure your application has an App.config file. If it is missing, add it by going to Project &gt; Add New Item&#8230; and then selecting &#8220;Application Configuration File&#8221; from the pop up. Visual Studio will then add a blank App.config file to your application. Open it and make it look something like this:</p>
<p><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;<br />
&lt;configuration&gt;<br />
&lt;appSettings&gt;<br />
&lt;add key="PreferenceToRemember" value="DefaultValue" /&gt;<br />
&lt;/appSettings&gt;<br />
&lt;/configuration&gt;</code></p>
<p>In order to write to this App.config file, you will need to add System.Configuration as a project reference. And of course you&#8217;ll need to have the appropriate <code>usings</code> statements on the right forms. Once all that is done, you can write to the App.config file with the following:</p>
<p><code>Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);<br />
oConfig.AppSettings.Settings["PreferenceToRemember"].Value = "NewValue";<br />
oConfig.Save(ConfigurationSaveMode.Modified);<br />
ConfigurationManager.RefreshSection("appSettings");</code></p>
<p>The last line in the code above refreshes the current in-memory configuration with what is saved inside the App.config file. To read from the App.config file, just use this:</p>
<p><code>string strPreferenceToRemember = ConfigurationManager.AppSettings["LastProject"];</code></p>
<p>As a final note, I noticed that nothing seemed to be written to the configuration file while debugging within Visual Studio, but once I published the application it worked as expected. I don&#8217;t know why this happens, but I&#8217;ll modify this post if I find out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kylirhorton.com/2008/read-and-write-settings-in-appconfig/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
