How to change location of app.config

后端 未结 4 629
甜味超标
甜味超标 2020-12-15 11:51

I want to change the location where my application looks for the app.config file.

I know that I can use ConfigurationManager.OpenExeConfiguration() to access an ar

4条回答
  •  佛祖请我去吃肉
    2020-12-15 12:22

    Another approach is to leave the config file with the executable file and move the relevant changeable sections to external xml files which can be in whatever location you choose.

    If you are using your config file in a readonly capacity, then you can add the relevant chunks to an XML file in a different location using XML Inlcude. This won't work if you are trying to write values back directly to app.config using the Configuration.Save method.

    app.config:

    
    
        
          
        
      
        
      
    
    

    ConnectionStrings.xml:

    
    
    

    AppSettings.xml:

    
    
    

    A file URI looks like this:

    file:///C:/whatever.txt
    

    You can even define failover files in case the one you are trying to reference is missing. This pattern is from https://www.xml.com/pub/a/2002/07/31/xinclude.html:

    
      
        
          This administration is doing everything we can to end the stalemate in
          an efficient way. We're making the right decisions to bring the solution
          to an end.
        
      

提交回复
热议问题