从applicationSettings读取设置非常慢?

时间:2015-06-01 15:16:42

标签: c# performance settings application-settings

我有一个C#WinForms应用程序,它在我的App.config文件中存储了4个设置(特别是在applicationSettings部分)。这些设置是通过项目创建的 - >属性 - > Visual Studio中的设置UI。我的App.config文件如下所示:

<?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>

当我的应用程序从我的主窗体的Load()事件处理程序启动时,我读取了这些设置,如下所示:

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;
    }
}

我已经在运行Windows 7 x86的测试计算机上运行Visual Studio 2013中的应用程序,一切都运行良好。今天早上,我构建了我的应用程序安装程序,它部署了应用程序,配置文件,.NET 4.5.2等。然后我将我的应用程序安装在相同的测试机器上(没有安装Visual Studio)并运行它。令我惊讶的是,在我的主要表格出现之前花了将近20秒。在对这个问题进行了一段时间的撞击之后,我下载了JetBrains的试用版&#39;优秀的dotTrace探查器,并在测试机器上对我的应用程序运行它。

至少可以说,我很惊讶看到对ReadSettingsFromAppConfig()的调用需要将近20秒才能完成!我已多次运行探查器,每次调用ReadSettingsFromAppConfig()需要20秒加上或减去100 - 200毫秒。我完全失去了导致这种情况的原因,因为当应用程序在Visual Studio中运行时我没有观察到这种行为。这些设置最终被正确读取,但似乎只是花费了大量的时间才能实现。

我还在运行Windows 7 x64以及Windows 8.1 x64的VM中测试了我的应用程序,并立即启动应用程序。虽然听起来很疯狂,但我想知道它是否与测试机器上的平台有关(x86而不是x64)。 FWIW,我的应用程序是针对&#34;任何CPU&#34;而构建的。有没有人使用applicationSettings体验过这种行为?

1 个答案:

答案 0 :(得分:1)

如果您使用以下开销较少:

<appSettings> 

而不是:

<applicationSettings>

然而,你失去了一些好处(例如类型检查),所以这是一个让步。