2

I am looking to write code to open and edit the base web.config files for a machine (as part of an installer for an HttpModule).

the files i want to edit are located commonly at:

  • C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
  • C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\web.config

If i use the following code i can open the 32bit version of the file:

Configuration configuration = WebConfigurationManager.OpenWebConfiguration(null);

How do i open the 64 bit version of the file? (regardless of where it has been installed, even if it is installed elsewhere on a machine).

UPDATE: The code in question is for an installer that adds an GAC assembly reference, HttpModule, and HttpHandler to the base web.config of a machine. As the Module is designed to be used on a "whole server" basis, the base web.config is the best place to put this. I REPEAT: THIS IS NOT FOR THE CREATION OF A VIRUS AS PROPOSED IN COMMENTS. The very fact that you need to be running elevated to touch these files should help make this this assumption false.

Doug
  • 6,460
  • 5
  • 59
  • 83
  • 1
    I could be way off base here, but why do you need to edit this file? Can't you override the settings with your local `web.Config`? – Chase Florell Nov 15 '11 at 00:52
  • I am using this in an windows installer that adds an HttpModule and HttpHandler to the _base_ web.config so that all sites on a single server have them installed by default – Doug Nov 15 '11 at 01:06
  • 2
    @Doug - I don't know about you, but if I found out an app I installed did something like that globally, i'd consider it a virus and delete it from my system. People do not take kindly to taking over all sites. – Erik Funkenbusch Nov 15 '11 at 01:10
  • 1
    Except the project i am creating is for sysadmins who are installing the product for this very purpose. The installer is there to save them time and avoid misconfigurations to the base config. Please stay on topic and avoid making assumptions. – Doug Nov 15 '11 at 01:27
  • @MystereMan Oracle 10g and 11g providers for ASP.NET do this. [see here](http://download.oracle.com/docs/html/E10928_01/IntroInstallation.htm) I've had issues with it before and I really hate that Oracle does this. – Joseph Yaduvanshi Nov 15 '11 at 19:44

1 Answers1

2
XmlTextReader reader = new XmlTextReader(FILE_NAME);
XmlDocument doc = new XmlDocument(); 
doc.Load(reader);
reader.Close();

//Select the cd node with the matching title
XmlNode oldCd;
XmlElement root = doc.DocumentElement;
oldCd = root.SelectSingleNode("/catalog/cd[title='" + oldTitle + "']");

XmlElement newCd = doc.CreateElement("cd");
newCd.SetAttribute("country",country.Text);

newCd.InnerXml = "<title>" + this.comboBox1.Text + "</title>" + 
        "<artist>" + artist.Text + "</artist>" +
        "<price>" + price.Text + "</price>";

root.ReplaceChild(newCd, oldCd);

//save the output to a file
doc.Save(FILE_NAME);
MethodMan
  • 18,625
  • 6
  • 34
  • 52