C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作

简介: C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作

应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config)。

配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET 2.0 开始,就非常方便了,提供了 System [.Web] .Configuration 这个管理功能的NameSpace,要使用它,需要添加对 System.configuration.dll的引用。

对于WINFORM程序,使用 System.Configuration.ConfigurationManager;

对于ASP.NET 程序, 使用 System.Web.Configuration.WebConfigurationManager;

对于配置文件内容的读取,真是太普遍不过了,如果你的程序里,没有读取配置文件内容的方面,你都不好意思拿出来用

我们以最常见的 AppSettings 小节来作为例子:

假设有如下的配置文件内容:

<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="y" value="this is Y"/> </appSettings></configuration>

1. 读取值:

  • Asp.Net:   System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];
  • WinForm: System.Configuration.ConfigurationManager.AppSettings[“y”];

2. 添加一项

  • ASP.NET(需要有写权限):

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);AppSettingsSection app = config.AppSettings;app.Settings.Add("x", "this is X");config.Save(ConfigurationSaveMode.Modified);

  • WinForm:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);AppSettingsSection app = config.AppSettings;app.Settings.Add("x", "this is X");config.Save(ConfigurationSaveMode.Modified);

3. 修改一项

  • Asp.Net

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);AppSettingsSection app = config.AppSettings;//app.Settings.Add("x", "this is X");app.Settings["x"].Value = "this is not Y";config.Save(ConfigurationSaveMode.Modified);

  • WinForm

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);AppSettingsSection app = config.AppSettings;//app.Settings.Add("x", "this is X");app.Settings["x"].Value = "this is not Y";config.Save(ConfigurationSaveMode.Modified);

4. 删除一项

  • Asp.Net

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);AppSettingsSection app = config.AppSettings;app.Settings.Remove("x");config.Save(ConfigurationSaveMode.Modified);

  • WinForm

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);AppSettingsSection app = config.AppSettings;app.Settings.Remove("x");config.Save(ConfigurationSaveMode.Modified);

相关文章
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
1195 5
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
363 2
|
API C#
异步轮询 Web API 的实现与 C# 示例
异步轮询 Web API 的实现与 C# 示例
586 1
|
前端开发 JavaScript C#
C#开发者的新天地:Blazor如何颠覆传统Web开发,打造下一代交互式UI?
【8月更文挑战第28天】Blazor 是 .NET 生态中的革命性框架,允许使用 C# 和 .NET 构建交互式 Web UI,替代传统 JavaScript。本文通过问答形式深入探讨 Blazor 的基本概念、优势及应用场景,并指导如何开始使用 Blazor。Blazor 支持代码共享、强类型检查和丰富的生态系统,简化 Web 开发流程。通过简单的命令即可创建 Blazor 应用,并利用其组件化和数据绑定特性快速搭建界面。无论对于 .NET 还是 Web 开发者,Blazor 都是一个值得尝试的新选择。
1268 1
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
2121 0
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
341 0
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
238 0
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
407 0
|
存储 安全 网络安全
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
331 0
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
251 0