所以我试图将连接字符串传递到我的WPF应用程序中,并且通过每个How-To我检查它的一个简单过程。转到App.config并输入相应的元素。之后我尝试获取值但结果总是为空。没有名称,没有键,没有值。我确保在我的参考资料中加入了System.Configuration。我甚至可以查看我的app.exe.config,看到我想要工作的元素,但它仍然找不到它。重建。ReRestart VS2019。仍然为空。谁能告诉我我做错了什么吗?
AppConfig
<configuration>
........
<connectionStrings>
<add name="BasePath" connectionString="CString" />
</connectionStrings>
<configuration>
在cs文件
中public static string GetConnection()
{
string result = null;
System.Configuration.Configuration config = ConfigurationManager.
OpenExeConfiguration("Filepathapp.exe");
ConnectionStringsSection section =
config.GetSection("connectionStrings")
as ConnectionStringsSection;
// Note: I have tried simply using ConfigurationManager.ConnectionStrings too
// There is a system defined connection string so its not null for that
if (section != null)
{
foreach (ConnectionStringSettings cs in section.ConnectionStrings)
{
// It doesn't even find cs with a name=BasePath
if (cs.Name == "BasePath")
{
return result = "CString";
}
}
}
return result;
}
试试这个方法
string connectionSrt = System.Configuration.ConfigurationManager.ConnectionStrings["BasePath"].ConnectionString;
问候。