问题
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.
回答1:
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);
来源:https://stackoverflow.com/questions/8130085/opening-the-machine-base-web-config-64bit-through-code