如何使用C#编辑Web.config文件 [英] How to edit the Web.config file using c#

查看:297
本文介绍了如何使用C#编辑Web.config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c://inetpub/wwroot/Example/Web.config的根目录中有web.config文件.此web.config文件包含ConnectionStrings和Appsettings值.现在,我想通过编程方式使用c#来修改web.config appsettings和连接字符串值.请帮我.

编辑-
[OP的其他注释已从解决方案"部分移至此处. ]

我在c://Inetpub/wwwroot/MyInstaller中有web.config文件.当我打开文件时,此处不允许出现诸如相对虚拟路径''C:/Inetpub/wwwroot/MyInstaller"之类的引发错误.

I have the web.config file in the root of c://inetpub/wwroot/Example/Web.config. This web.config file contains ConnectionStrings and Appsettings value. Now i want to modify the web.config appsettings and connection strings value by programmatically using c#. Pleas help me.

Edit -
[ OP''s additional comments moved here from Solution section. ]

I have the web.config file in c://Inetpub/wwwroot/MyInstaller. When i am opening a file Its throwing error like "The relative virtual path ''C:/Inetpub/wwwroot/MyInstaller'' is not allowed here.

推荐答案

已看看下面类似的问答"讨论.

通过C#代码读取和修改Web.Config文件

使用C#(.NET)以编程方式更改web.config
Have a look at similar Question-Answer discussions below.

Read and Modify Web.Config file through C# code

Change a web.config programmatically with C# (.NET)


此文件是XML文件,.NET很好地支持读/写XML.从下面概述的方法中进行选择:

XML读/写:
This file is an XML file, and reading/writing XML is well supported by .NET. Choose from the methods I overview below:

XML Read/Write:

  1. 使用System.Xml.XmlDocument类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
    请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].



由于此文件通常很小,所以我选择XmlDocument,这可能是最简单的.您将必须将整个文件读入DOM,是否使用DOM接口进行了更改,然后保存了整个文件.

—SA



As this file is usually quite small, I would choose XmlDocument, which is probably the easiest. You will have to read the whole file into DOM, do you changed using DOM interface and then save the whole file.

—SA


您尝试过使用google吗?提供了许多示例.

您需要使用WebConfigurationManager并打开配置文件.然后获取您要修改和更新的部分.请参阅下面的链接

点击此处

单击此处2
Did you try google? Lots of examples are available.

You need to use WebConfigurationManager and open the config file. And later get the section, which you want to modify and update it. refer the links below

Click here

Click here 2


这篇关于如何使用C#编辑Web.config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆