如何使用 app.config 将我的 C# 控制台应用程序设置为 运行?

How do I get my c# console application to run with an app.config?

这对我来说很特别,但我确信我遗漏了一些简单和基本的东西。我有一个控制台应用程序,它从 app.config 读取 SQL 连接。它在 Visual Studio 中运行良好。当我将三个项目移动到一个目录进行测试时,.exe、app.config 和 crystal 报告它需要运行,它不会从 app.config 中读取。

Console.WriteLine("about to get connection string...");
var connectionString = ConfigurationManager.ConnectionStrings["InformConnectionString"].ConnectionString;

它在 var connectionString 行上爆炸,出现未处理的异常对象引用未设置为对象的实例。同样,在开发环境中工作正常,只是在生产目录中不行。

因为app.config在编译的时候改名为YourAppName.exe.config。查看您的 bin/debug 文件夹或 bin/release 文件夹。在那里你可以找到使用过的配置文件