wpf - Get values from App Config file

别说谁变了你拦得住时间么 提交于 2019-12-04 04:39:22

If you expand the Properties section of Visual Studio and double click the settings section, you will be able to add custom settings which end up like so in the config file:

<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="WpfApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<userSettings>
    <WpfApplication1.Properties.Settings>
        <setting name="FilePath" serializeAs="String">
            <value>Thing</value>
        </setting>
    </WpfApplication1.Properties.Settings>
</userSettings>
</configuration>

Which you can then do this in your code:

string thing = Properties.Settings.Default.FilePath;

Which is nice because it gives you type safety too

The code you wrote should work - make sure you haven't changed 'BuildAction' of the config file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!