Opening the machine/base Web.Config (64bit) through code

孤人 提交于 2019-12-06 09:48:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!