How to find path of active app.config file?

How to find path of active app.config file?

一个人的旅程 发布于 2021-11-27 字数 530 浏览 739 回复 8 原文

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.

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



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


爱殇璃 2022-06-07 8 楼

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 FilesIIS ExpressMyComponent.dll.config. Interesting place for it.

独孤求败 2022-06-07 7 楼

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

薯片软お妹 2022-06-07 6 楼

One more option that I saw is missing here:

string defaultSysConfigFilePath = (string)AppDomain.CurrentDomain.GetData(APP_CONFIG_FILE);
像极了他 2022-06-07 5 楼

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.

一抹苦笑 2022-06-07 4 楼

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

梦与时光遇 2022-06-07 3 楼

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

月亮是我掰弯的 2022-06-07 2 楼

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):


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.

空袭的梦i 2022-06-07 1 楼

Try this