简体   繁体   中英

Moving app.config file to a custom location

My application is a service application, and it needs to load a set of configuration that the user can change. I was using the System.Configuration classes to set up the app.config file, and then I was going to give the user the option of editing this file to change configuration.

Can I move this file to a custom directory, something like C:\\settings\\app.config? Or is it forced to reside in the app directory?

Thank you

Take a look at this link:

How to read app.config from a custom location, ie from a database in .NET

this answer in particular:

What about using:

System.Configuration.ConfigurationManager.OpenExeConfiguration(string exePath)
That should let you open an arbitrary app.config file.

the file you see now as app.config will be named myservice.exe.config and will be available in the same folder of the executable myservice.exe, for what I know of windows applications, opposite than ASP.NET application, while the program is running you cannot edit manually the .config file, will be locked and if you open it with notepad you cannot save. Even in case you do some magic to unlock it, then you should still stop and restart the windows service to grab the latest values from there. I do not know if you could put it in another folder, I guess not. You could still save the required settings in some other files, xml or in the database, and your .config file would contain a key with the path of those custom XML files, for example.

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:

 <?xml version="1.0"?> <configuration xmlns:xi="http://www.w3.org/2001/XInclude"> <appSettings> <xi:include href="AppSettings.xml"/> </appSettings> <connectionStrings> <xi:include href="ConnectionStrings.xml"/> </connectionStrings> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/></startup> </configuration> 

ConnectionStrings.xml:

 <?xml version="1.0"?> <add name="Example1ConnectionString" connectionString="Data Source=(local)\\SQLExpress;Initial Catalog=Example1DB;Persist Security Info=True;User ID=sa;Password=password" providerName="System.Data.SqlClient" /> <add name="Example2ConnectionString" connectionString="Data Source=(local)\\SQLExpress;Initial Catalog=Example2DB;Persist Security Info=True;User ID=sa;Password=password" providerName="System.Data.SqlClient" /> 

AppSettings.xml:

 <?xml version="1.0"?> <add key="Setting1" value="Value1"/> <add key="Setting2" value="Value2"/> 

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 :

 <xi:include href="http://www.whitehouse.gov/malapropisms.xml"> <xi:fallback> <para> 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. </para> </xi:fallback> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM