ConfigurationManager.AppSettings use another config file

前端 未结 3 438
既然无缘
既然无缘 2020-12-16 20:22

I have about 10 methods in my class. In every method I use ConfigurationManager.AppSettings to get value form App.config file

like

 _a         


        
相关标签:
3条回答
  • 2020-12-16 20:50

    You can accomplish this by using ConfigurationManager.OpenExeConfiguration. This will allow you to open another configuration file easily.

    MSDN article about OpenExeConfiguration.

    0 讨论(0)
  • 2020-12-16 20:55

    You could do something like this

    var fileConfig = ConfigurationManager.OpenExeConfiguration("<filePath>");
    int port = int.Parse(fileConfig.AppSettings["PortNumber"].ToString());
    
    0 讨论(0)
  • 2020-12-16 21:03

    You can also set the app.config to read another file. Something like this:

    <?xml version="1.0"?>
    <configuration>
      <appSettings  file="my\custom\file\path\external.config"/>
    </configuration>
    

    and the external.config will have the appSettings section:

    <appSettings>
        <add key="myKey" value="myValue" />
    </appSettings>
    

    refer to this msdn for additional info.

    0 讨论(0)
提交回复
热议问题