简体   繁体   中英

How to create a modified copy of machine.config file?

I want to add/remove/upsert a line for ADO .NET data provider programmatically with C#.

My first thought was to parse the file with some parser (like Eto.Parse), then add/remove necessary span of text and then write a new file into install image directory (which is not write protected unlike to write protected main machine.config).

Then I think, that the file is xml, and it is possible to use existing xml machinery instead of custom parser. Load XML, build object model from XML, modify it and serialize.

Then I realise, that object model for working with configs is already present in System.Configuraion namespace.

And I decide to search an existing example on how to modify the machine config with these classes. I found only an example how to obtain it's location new ConfigurationFileMap().MachineConfigFilename; (see The best way to get a path to machine.config of a different .NET version )

Just tell the ConfigManager that you are looking to edit something other than the current app's config file.

Configuration config = ConfigurationManager.OpenMachineConfiguration(); 

The, you can use config.sections[whatever] to access specific sections.

Keep in mind that the config object maps out most of the properties you are attempting to tweak, so you'll need to dig through the specific section's interface to find exactly what you're trying to mess-up update.

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