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