c# - 通过代码打开机器/基地Web.Config(64位)

标签 c# asp.net web-config machine.config

我正在寻找代码来打开和编辑机器的基本 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 是放置它的最佳位置。我再说一遍:这不是为了创建评论中提议的病毒。您需要提升运行才能接触这些文件这一事实应该有助于使这个假设错误。

最佳答案

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);

关于c# - 通过代码打开机器/基地Web.Config(64位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8130085/

相关文章:

c# - 如何使用 OpenSSL.Net C# 包装器通过 AES 加密字符串?

c# - 如何在 ASP.NET 中通过 C# 从数据库中的列获取平均值?

asp.net - Visual Studio 的 "Publish"功能阻止我的 ASP.NET MVC 应用程序编译

url - 使用 web.config 从 Windows 主机帐户的 URL 中删除 php 扩展

c# - 如何从 C# 使用 CHKDSK 工具并在 CMD 窗口中显示文本?

c# - 使用自定义类扩展类型

asp.net - 规范化数据库所需的建议

php - 从 PHP 应用程序的 Windows 身份验证 IIS 中提取登录信息

c# - 如何在 Xamarin.Forms 的 ListView 中获取所选项目的坐标?

javascript - 无法在javascript中获取文本框值