如何查找活动 app.config 文件的路径?

发布于 2024-07-17 23:09:18 字数 467 浏览 3 评论 0原文

我正在尝试完成这个异常处理程序:

if (ConfigurationManager.ConnectionStrings["ConnectionString"]==null)
{
    string pathOfActiveConfigFile = ...?
    throw new ConfigurationErrorsException(
       "You either forgot to set the connection string, or " +
       "you're using a unit test framework that looks for  "+
       "the config file in strange places, update this file : " 
       + pathOfActiveConfigFile);
}

这个问题似乎只在我使用 nUnit 时发生在我身上。

I'm trying to finish this exception handler:

if (ConfigurationManager.ConnectionStrings["ConnectionString"]==null)
{
    string pathOfActiveConfigFile = ...?
    throw new ConfigurationErrorsException(
       "You either forgot to set the connection string, or " +
       "you're using a unit test framework that looks for  "+
       "the config file in strange places, update this file : " 
       + pathOfActiveConfigFile);
}

This problem seems to only happen to me when I'm using nUnit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

清泪尽 2024-07-24 23:09:18

对于 .Net Framework,请尝试此

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

对于 .Net core 或任何更新的内容,请参阅其他答案。

For .Net Framework, try this

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

For .Net core or anything newer, see the other answers.

坏尐絯 2024-07-24 23:09:18

严格来说,不存在单一的配置文件。 除了 ASP.NET1 之外,还可以使用内置 (System.Configuration) 支持创建三个配置文件。 除了计算机配置:app.exe.config、用户漫游和用户本地。

要获取“全局”配置 (exe.config):

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                    .FilePath

使用不同的 每次使用的漫游和非漫游配置文件的ConfigurationUserLevel值。


1 具有完全不同的模型,其中子文件夹(IIS 虚拟或文件系统)web.config 的内容可以(取决于设置)添加或覆盖父级的 web.config

Strictly speaking, there is no single configuration file. Excluding ASP.NET1 there can be three configuration files using the inbuilt (System.Configuration) support. In addition to the machine config: app.exe.config, user roaming, and user local.

To get the "global" configuration (exe.config):

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                    .FilePath

Use different ConfigurationUserLevel values for per-use roaming and non-roaming configuration files.


1 Which has a completely different model where the content of a child folders (IIS-virtual or file system) web.config can (depending on the setting) add to or override the parent's web.config.

旧时浪漫 2024-07-24 23:09:18

如果您的意思是在使用 NUnit 时仅获得 null 返回,那么您可能需要将应用程序的 app.config 中的 ConnectionString 值复制到测试库的 app.config 中。

当测试加载器运行时,测试程序集在运行时加载,并将查找其自己的 app.config(在编译时重命名为 testAssembly.dll.config),而不是您的应用程序配置文件。

要获取您正在运行的程序集的位置,请尝试

System.Reflection.Assembly.GetExecutingAssembly().Location

If you mean you are only getting a null return when you use NUnit, then you probably need to copy the ConnectionString value the your app.config of your application to the app.config of your test library.

When it is run by the test loader, the test assembly is loaded at runtime and will look in its own app.config (renamed to testAssembly.dll.config at compile time) rather then your applications config file.

To get the location of the assembly you're running, try

System.Reflection.Assembly.GetExecutingAssembly().Location
眼眸印温柔 2024-07-24 23:09:18

确保单击文件上的属性并将其设置为“始终复制”,否则它将不会与您满意的 lil dll 一起位于 Debug\ 文件夹中,以配置它需要的位置并添加更多的牛铃

Make sure you click the properties on the file and set it to "copy always" or it will not be in the Debug\ folder with your happy lil dll's to configure where it needs to be and add more cowbell

迷路的信 2024-07-24 23:09:18

我第一次意识到单元测试项目引用了该项目中的 app.config,而不是与我的生产代码项目关联的 app.config(当然,DOH),我只是在 Prod 项目的构建后事件中添加了一行这会将 app.config 复制到测试项目的 bin 文件夹中。

问题已解决

到目前为止,我还没有注意到任何奇怪的副作用,但我不确定这是正确的解决方案,但至少它似乎有效。

The first time I realized that the Unit testing project referenced the app.config in that project rather then the app.config associated with my production code project (off course, DOH) I just added a line in the Post Build Event of the Prod project that will copy the app.config to the bin folder of the test project.

Problem solved

I haven't noticed any weird side effects so far, but I am not sure that this is the right solution, but at least it seems to work.

蹲墙角沉默 2024-07-24 23:09:18

我看到这里缺少另一个选项:

const string APP_CONFIG_FILE = "APP_CONFIG_FILE";
string defaultSysConfigFilePath = (string)AppDomain.CurrentDomain.GetData(APP_CONFIG_FILE);

One more option that I saw is missing here:

const string APP_CONFIG_FILE = "APP_CONFIG_FILE";
string defaultSysConfigFilePath = (string)AppDomain.CurrentDomain.GetData(APP_CONFIG_FILE);
神经暖 2024-07-24 23:09:18

根据配置文件的位置,System.Reflection.Assembly.GetExecutingAssembly().Location 可能会满足您的需要。

Depending on the location of your config file System.Reflection.Assembly.GetExecutingAssembly().Location might do what you need.

坐在坟头思考人生 2024-07-24 23:09:18

我在 Web 应用程序(实际上是本地运行的 Azure Web 角色)中尝试了之前的答案之一,但它不太有效。 然而,这种类似的方法确实有效:

var map = new ExeConfigurationFileMap { ExeConfigFilename = "MyComponent.dll.config" };
var path = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).FilePath;

配置文件位于 C:\Program Files\IIS Express\MyComponent.dll.config 中。 有趣的地方。

I tried one of the previous answers in a web app (actually an Azure web role running locally) and it didn't quite work. However, this similar approach did work:

var map = new ExeConfigurationFileMap { ExeConfigFilename = "MyComponent.dll.config" };
var path = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).FilePath;

The config file turned out to be in C:\Program Files\IIS Express\MyComponent.dll.config. Interesting place for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文