码迷,mamicode.com
首页 > 其他好文 > 详细

如何获取配置文件的路径

时间:2016-03-16 12:12:04      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

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  的路径是一致的

 

如何获取配置文件的路径

标签:

原文地址:http://www.cnblogs.com/chucklu/p/5282689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!