ConfigurationManager in WPF

北慕城南 提交于 2019-12-01 16:18:35

Just add an app.config and not web.config because it is not a web application.

And after that it's too simple, just add a reference to to System.Configuration and then use this.

var ConnStr = ConfigurationManager.AppSettings["Trackboard"];

I've figured it out! I shouldn't have created a new config file, there is a default app.config file in project. Now everything is fine. Thank you all!

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Trackboard.Properties.Settings.TrackboardConnectionString"
            connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\DATABASE\Trackboard.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

private static string ConnStr = ConfigurationManager.ConnectionStrings["Trackboard.Properties.Settings.TrackboardConnectionString"].ConnectionString;

This one use System.Configuration namespace

using System.Configuration;


Or add System.Configuration in reference

System.ConfigurationManager.ConnectionStrings["Trackboard"].ConnectionString;
System.ConfigurationManager.ConnectionStrings[0].ConnectionString;

You have to reference the System.Configuration assembly which is in GAC

Use of ConfigurationManager is not WPF specific : it is the priviledged way to access configuration information for any type of application

Please see MSDN for further info

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