<?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>Eclipsed4utoo&#039;s Blog&#187; app.config</title>
	<atom:link href="http://eclipsed4utoo.com/blog/tag/app-config/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsed4utoo.com/blog</link>
	<description>Not Your Ordinary Programmer</description>
	<lastBuildDate>Fri, 23 Jul 2010 18:10:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WPF &#8211; Using an Application Configuration File</title>
		<link>http://eclipsed4utoo.com/blog/wpf-application-configuration-file/</link>
		<comments>http://eclipsed4utoo.com/blog/wpf-application-configuration-file/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 19:16:01 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[app.config]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ConfigurationManager]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=292</guid>
		<description><![CDATA[This is going to be a real short post about an issue that I ran into recently with WPF.
I was wanting to use an app config file with my WPF application.  I was running into an issue with the ConfigurationManager calls not getting the information from the app config file.
I added the app config just [...]]]></description>
			<content:encoded><![CDATA[<p>This is going to be a real short post about an issue that I ran into recently with WPF.</p>
<p>I was wanting to use an app config file with my WPF application.  I was running into an issue with the ConfigurationManager calls not getting the information from the app config file.</p>
<p>I added the app config just like I would in a Windows Form..<br />
<img class="alignnone size-full wp-image-293" title="AppConfig" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/02/AppConfig.png" alt="AppConfig" width="679" height="418" /></p>
<p>My app config file looked something like this&#8230;</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;configuration&gt;
  &lt;appSettings&gt;
     &lt;add key=&quot;myKey&quot; value=&quot;SomeValue&quot; /&gt;
  &lt;/appSettings&gt;
&lt;/configuration&gt;
</pre>
<p>I was using the standard code that works fine in Windows Forms(needed to add a reference to the System.Configuration):</p>
<pre class="brush: csharp;">
using System.Configuration

private void Window_Loaded(object sender, RoutedEventArgs e)
{
     var myKey = ConfigurationManager.AppSettings[&quot;myKey&quot;];
}
</pre>
<p>However, the app settings were not being found.  After scratching my head for a while and searching numerous places online, I found a small response on a forum.  None of the &#8220;MVP&#8221;s picked up on it.  It was because of the name of the app config file.  By default, VS2008 added the file as &#8220;App1.config&#8221;.  For some reason, the ConfigurationManager class looks for a file named exactly &#8220;App.config&#8221;.</p>
<p>So simply changing the name of the config file to &#8220;App.config&#8221; fixed the issue and the code started working.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wpf-application-configuration-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# &#8211; Read Configuration File from Another Running Application</title>
		<link>http://eclipsed4utoo.com/blog/read-configuration-file-running-application/</link>
		<comments>http://eclipsed4utoo.com/blog/read-configuration-file-running-application/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 15:32:26 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[app.config]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[configuration]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=160</guid>
		<description><![CDATA[I recently was writing an small application that would be running in the background to complement my main application that users were going to be interacting with.  Both applications would be connecting to a database, so I needed a connection string that was configurable by users.  In my main application, I already had this working [...]]]></description>
			<content:encoded><![CDATA[<p>I recently was writing an small application that would be running in the background to complement my main application that users were going to be interacting with.  Both applications would be connecting to a database, so I needed a connection string that was configurable by users.  In my main application, I already had this working fine.  However, I didn&#8217;t want the users to have to configure both applications.  So I decided that I was just going to read the app.config file of my main application and use it&#8217;s connection string for the application running in the background.</p>
<p>This is how I accomplished it.</p>
<p>So first, I need to get the startup location of my main application.  It could be installed anywhere, so I can&#8217;t hard-code the path.  I also know that the main application will always be running.  So I use the <strong>Process</strong> class to get an array of the current processes running on the machine.  I then loop through those processes, looking for the process that has the name of my application.</p>
<pre class="brush: csharp;">
// Add these using statements at the top of the code page(if not already there)
using System.Configuration;
using System.Diagnostics;

string exePath = string.Empty;
Process[] processes = Process.GetProcesses();

// Alternatively, this could have also been used
// Process[] processes = Process.GetProcessesByName(&quot;myApplicationName&quot;);

foreach (Process p in processes)
{
     // try/catch was used because some applications threw a &quot;Access Denied&quot;
     //     when trying to read it's filename.
     try
     {
        if (p.ProcessName == &quot;myApplicationName&quot;)
        {
             exePath = p.MainModule.FileName;
             break;
        }
     }
     catch {}
}
</pre>
<p>Now I need to read the configuration file for the application&#8217;s exe.</p>
<pre class="brush: csharp;">
string connectionString = string.Empty;

Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
KeyValueConfigurationCollection col = config.AppSettings.Settings;
connectionString = col[&quot;Main.ConnectionString&quot;].Value;
</pre>
<p>And that&#8217;s all I needed to do.  Now my users only need to configure the main application, and I can still connect to the database using both applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/read-configuration-file-running-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
