2

我正在寻找编写代码来打开和编辑机器的基本 web.config 文件(作为 HttpModule 安装程序的一部分)。

我要编辑的文件通常位于:

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

如果我使用以下代码,我可以打开文件的 32 位版本:

Configuration configuration = WebConfigurationManager.OpenWebConfiguration(null);

如何打开文件的 64 位版本?(不管它安装在哪里,即使它安装在机器上的其他地方)。

更新:有问题的代码用于将 GAC 程序集引用、HttpModule 和 HttpHandler 添加到机器的基本 web.config 的安装程序。由于该模块被设计为在“整个服务器”的基础上使用,因此基本的 web.config 是放置它的最佳位置。我重复一遍:这不是为了创建评论中建议的病毒。您需要运行提升才能接触这些文件这一事实应该有助于使这种假设成为错误。

4

1 回答 1

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);
于 2011-11-15T19:41:50.033 回答