如何获取配置文件的路径
ConfigurationManager.OpenExeConfiguration Method
Opens the specified client configuration file as a Configuration object.
http://stackoverflow.com/questions/793657/how-to-find-path-of-active-app-config-fil
第一个回答:
Try this
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
Hope it helps
第二个回答:
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.
Examples
Console.WriteLine($"AppDomain.CurrentDomain.SetupInformation.ConfigurationFile = {AppDomain.CurrentDomain.SetupInformation.ConfigurationFile}"); Console.WriteLine(); var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine($"ConfigurationUserLevel.None = {configFile.FilePath}"); Console.WriteLine(); configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming); Console.WriteLine($"ConfigurationUserLevel.PerUserRoaming = {configFile.FilePath}"); Console.WriteLine(); configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); Console.WriteLine($"ConfigurationUserLevel.PerUserRoamingAndLocal = {configFile.FilePath}"); Console.WriteLine();
exe文件所处路径 d/SourceCode/CSharp/ConsoleTest/ConsoleApplication1/bin/Debug
输出结果如下:
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile = D:\SourceCode\CSharp\ConsoleTest\ConsoleApplication1\bin\Debug\ConsoleApp.exe.Config
ConfigurationUserLevel.None = D:\SourceCode\CSharp\ConsoleTest\ConsoleApplication1\bin\Debug\ConsoleApp.exe.Config
ConfigurationUserLevel.PerUserRoaming = C:\Users\Administrator\AppData\Roaming\ChuckLu\ConsoleApp.exe_Url_3lsb0kxaaocmfdaqj3ouxzvvp3phypxi\1.0.0.0\user.config
ConfigurationUserLevel.PerUserRoamingAndLocal = C:\Users\Administrator\AppData\Local\ChuckLu\ConsoleApp.exe_Url_3lsb0kxaaocmfdaqj3ouxzvvp3phypxi\1.0.0.0\user.config
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile和ConfigurationUserLevel.None 的路径是一致的
ConfigurationManager.OpenMappedExeConfiguration Method
Opens the specified client configuration file as a Configuration object.
相关文章
- 16-spring学习-配置文件操作
- npm配置文件
- Spring配置文件总结
- Linux下如何查看定位当前正在运行的Nginx的配置文件
- .net core自定义读取配置文件
- nginx配置文件语法高亮的Sublime Text扩展
- 【MyBatis笔记02】MyBatis核心配置文件介绍
- 以Java属性文件的格式创建Hibernate的配置文件和DTD特殊符号作用
- @Value和@PropertySource实现*.properties配置文件读取过程和实现原理
- [Servlet3.0新功能]注释替代配置文件
- Linux下如何查看定位当前正在运行的Nginx的配置文件
- MyBatis是如何加载配置文件的2
- ssm配置文件回顾
- 配置文件详解之–yum-conf