C# 如何找到活动 app.config 文件的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/793657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find path of active app.config file?
提问by MatthewMartin
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.
这个问题似乎只发生在我使用 nUnit 时。
采纳答案by Cédric Rup
Try this
尝试这个
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
Hope it helps
希望能帮助到你
回答by Colin Desmond
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.
如果您的意思是在使用 NUnit 时只得到空返回,那么您可能需要将应用程序的 app.config 中的 ConnectionString 值复制到测试库的 app.config 中。
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.
当它由测试加载器运行时,测试程序集在运行时加载,并将查看它自己的 app.config(在编译时重命名为 testAssembly.dll.config)而不是您的应用程序配置文件。
To get the location of the assembly you're running, try
要获取您正在运行的程序集的位置,请尝试
System.Reflection.Assembly.GetExecutingAssembly().Location
回答by William Edmondson
Depending on the location of your config file System.Reflection.Assembly.GetExecutingAssembly().Location
might do what you need.
根据您的配置文件的位置,System.Reflection.Assembly.GetExecutingAssembly().Location
可能会满足您的需求。
回答by Chad Grant
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
确保单击文件上的属性并将其设置为“始终复制”,否则它不会与您的快乐 lil dll 一起位于 Debug\ 文件夹中,以配置它需要的位置并添加更多牛铃
回答by Richard
Strictly speaking, there is no single configuration file. Excluding ASP.NET1there 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.
严格来说,没有单一的配置文件。除了 ASP.NET 1 之外,还有三个使用内置 ( System.Configuration
) 支持的配置文件。除了机器配置:app.exe.config
,用户漫游和用户本地。
To get the "global" configuration (exe.config):
要获得“全局”配置(exe.config):
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
.FilePath
Use different ConfigurationUserLevel
values for per-use roaming and non-roaming configuration files.
ConfigurationUserLevel
为每次使用的漫游和非漫游配置文件使用不同的值。
1Which 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
.
1它具有完全不同的模型,其中子文件夹(IIS 虚拟或文件系统)的内容web.config
可以(取决于设置)添加或覆盖父文件夹的web.config
.
回答by Kasper
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.
我第一次意识到单元测试项目引用了该项目中的 app.config 而不是与我的生产代码项目关联的 app.config(当然,DOH)我只是在 Prod 项目的 Post Build Event 中添加了一行这会将 app.config 复制到测试项目的 bin 文件夹中。
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.
到目前为止,我还没有注意到任何奇怪的副作用,但我不确定这是正确的解决方案,但至少它似乎有效。
回答by RedGreenCode
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:
我在 Web 应用程序(实际上是在本地运行的 Azure Web 角色)中尝试了之前的答案之一,但效果不佳。但是,这种类似的方确实有效:
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.
配置文件原来在 C:\Program Files\IIS Express\MyComponent.dll.config 中。有趣的地方。
回答by selalerer
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);