0

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)

Community
  • 1
  • 1
user1709408
  • 528
  • 4
  • 16
  • 1
    It is weird that you want to touch machine.config as that affects all applications. You should only modify your own app.config or web.config whenever necessary. – Lex Li Jan 16 '16 at 11:24
  • 1
    what if he's coding an install script to config a bare-metal server? Do you STILL want to substitute your judgement for his? – bri Jan 16 '16 at 15:27

1 Answers1

1

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.

bri
  • 2,932
  • 16
  • 17