I have a C# WinForms application that stores 4 settings in my App.config file (specifically in the applicationSettings section). These settings were created via the Project -> Properties -> Settings UI in Visual Studio. My App.config file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyTestApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<applicationSettings>
<MyTestApp.Properties.Settings>
<setting name="LocalServerIpAddress" serializeAs="String">
<value>192.168.1.100</value>
</setting>
<setting name="LocalServerPort" serializeAs="String">
<value>59179</value>
</setting>
<setting name="RemoteServerIpAddress" serializeAs="String">
<value>192.168.1.200</value>
</setting>
<setting name="RemoteServerPort" serializeAs="String">
<value>59744</value>
</setting>
</MyTestApp.Properties.Settings>
</applicationSettings>
</configuration>
I read these settings when my application starts from the Load() event handler of my main form like this:
public partial class Form1 : Form
{
string LocalServerIpAddress = string.Empty;
string LocalServerPort = string.Empty;
string RemoteServerIpAddress = string.Empty;
string RemoteServerPort = string.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bool settingsLoaded = ReadSettingsFromAppConfig();
if (!settingsLoaded)
{
MessageBox.Show("An error occured while loading the application settings! Click OK to exit the application.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
}
private bool ReadSettingsFromAppConfig()
{
bool settingsLoaded = true;
try
{
This.LocalServerIpAddress = Properties.Settings.Default.LocalServerIpAddress;
This.LocalServerPort = Properties.Settings.Default.LocalServerPort;
This.RemoteServerIpAddress = Properties.Settings.Default.RemoteServerIpAddress;
This.RemoteServerPort = Properties.Settings.Default.RemoteServerPort;
}
catch (Exception)
{
settingsLoaded = false;
}
return settingsLoaded;
}
}
I've been running my application inside Visual Studio 2013 on a test machine which run Windows 7 x86 and everything has been working fine. This morning, I built my application installer which deploys the application, the config file, .NET 4.5.2, etc. I then installed my application on an identical test machine (which does not have Visual Studio installed) and ran it. To my surprise, it took nearly 20 seconds before my main form appeared. After banging my head against this problem for a while, I downloaded the trial version of JetBrains' excellent dotTrace profiler and ran it against my application on the test machine.
I was surprised, to say the least, to see the call to ReadSettingsFromAppConfig() taking nearly 20 seconds to complete! I've run the profiler multiple times and each time, the call to ReadSettingsFromAppConfig() is taking 20 seconds plus or minus 100 - 200 ms. I'm at a complete loss as to what would be causing this especially since I didn't observe this behavior when the application was running inside Visual Studio. The settings are ultimately being read correctly but it just seems to be taking an obscene amount of time for this to occur.
I also tested my application in a VM running Windows 7 x64 as well as Windows 8.1 x64 and the application starts immediately. As crazy as it sounds, I'm wondering if it's something related to the platform on the test machine (x86 instead of x64). FWIW, my application is built to target "Any CPU". Has anyone ever experienced this kind of behavior using applicationSettings?
There is less overhead if you use:
<appSettings>
Rather than:
<applicationSettings>
However, you lose some benefits (e.g. type checking), so it's a give-and-take.