zoukankan      html  css  js  c++  java
  • 如何获取配置文件的路径

    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:SourceCodeCSharpConsoleTestConsoleApplication1inDebugConsoleApp.exe.Config

    ConfigurationUserLevel.None =                                                  D:SourceCodeCSharpConsoleTestConsoleApplication1inDebugConsoleApp.exe.Config

    ConfigurationUserLevel.PerUserRoaming = C:UsersAdministratorAppDataRoamingChuckLuConsoleApp.exe_Url_3lsb0kxaaocmfdaqj3ouxzvvp3phypxi1.0.0.0user.config

    ConfigurationUserLevel.PerUserRoamingAndLocal = C:UsersAdministratorAppDataLocalChuckLuConsoleApp.exe_Url_3lsb0kxaaocmfdaqj3ouxzvvp3phypxi1.0.0.0user.config

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile和ConfigurationUserLevel.None  的路径是一致的

    ConfigurationManager.OpenMappedExeConfiguration Method

    Opens the specified client configuration file as a Configuration object.

     

  • 相关阅读:
    1、Spring Cloud Alibaba 新一代微服务解决方案
    springboot整合activemq(1)
    11.springboot+mybatis+dubbo+zookepper
    多线程的使用
    Spring 框架用到的 9 个设计模式汇总
    docker数据卷与数据卷容器
    4.GitHub的使用
    Spring Cloud中Hystrix、Ribbon及Feign的熔断关系
    git如何正确回滚代码
    1)Linux学习笔记:crontab命令
  • 原文地址:https://www.cnblogs.com/chucklu/p/5282689.html
Copyright © 2011-2022 走看看