权限管理、用户权限系统、开源用户权限系统、信息化建设标准基础数据管理平台
代码改变世界

C# 读取保存App.config配置文件的完整源码参考

  通用C#系统架构  阅读(16410)  评论(5编辑  收藏  举报

   最近出差在北京做一个小项目,项目里需要读取配置文件的小功能,觉得挺有参考意义的就把代码发上来给大家参考一下。我们选择了直接用微软的读取配置文件的方法。

 

这个是程序的运行设计效果,就是把这些参数可以进行灵活设置,灵活保存设置状态。

 

程序编译后自动会产生相应的配置文件,是跟项目的名称一样的配置文件。

 

读取配置文件及保存配置的具体代码参考如下,希望能给你节省一些时间,直接复制粘贴这个代码就可以用了:

复制代码
//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , CDPF , Ltd. 
//------------------------------------------------------------

using System;
using System.Configuration;
using System.Windows.Forms;
using Utilities;

namespace DirectSeeding
{
    
/// <summary>
    
///    FrmConfig
    
/// 读取配置文件
    
/// 
    
/// 修改纪录
    
/// 
    
///        2011.01.14 版本:   1.0 JiRiGaLa 完善程序的注释等、从新整理代码。
    
///    
    
/// 版本:1.0
    
///
    
/// <author>
    
///        <name>JiRiGaLa</name>
    
///        <date>2011.01.14</date>
    
/// </author> 
    
/// </summary>
    public partial class FrmConfig : Form
    {
        
public FrmConfig()
        {
            InitializeComponent();
        }

        
/// <summary>
        
/// 读取配置文件
        
/// </summary>
        private void GetConfig()
        {
            
this.txtWriteFileName.Text = ConfigurationManager.AppSettings["WriteFileName"];
            
this.txtWritePath.Text = ConfigurationManager.AppSettings["WritePath"].Replace("|", Environment.NewLine);
            
this.txtPostMessageURL.Text = ConfigurationManager.AppSettings["PostMessageURL"];
            
this.txtLeasedLineURL.Text = ConfigurationManager.AppSettings["LeasedLineURL"];
        }

        
private void FrmDirectSeeding_Load(object sender, EventArgs e)
        {
            
this.GetConfig();
        }

        
/// <summary>
        
/// 保存配置文件
        
/// </summary>
        private void SaveConfig()
        {
            
// 写入参数设置
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configuration.AppSettings.Settings[
"WriteFileName"].Value = this.txtWriteFileName.Text;
            configuration.AppSettings.Settings[
"WritePath"].Value = this.txtWritePath.Text.Trim().Replace(Environment.NewLine, "|");
            configuration.AppSettings.Settings[
"PostMessageURL"].Value = this.txtPostMessageURL.Text;
            configuration.AppSettings.Settings[
"LeasedLineURL"].Value = this.txtLeasedLineURL.Text;
            configuration.Save();

            
// 重新读取参数
            ConfigurationManager.RefreshSection("appSettings");
            WriteFile.WriteFileName 
= ConfigurationManager.AppSettings["WriteFileName"];
            WriteFile.WritePath 
= ConfigurationManager.AppSettings["WritePath"].Split('|');
            PostMessage.PostMessageURL 
= ConfigurationManager.AppSettings["PostMessageURL"];
            
// PostMessage.LeasedLineURL = ConfigurationManager.AppSettings["LeasedLineURL"];
        }

        
private void btnSavaConfig_Click(object sender, EventArgs e)
        {
            
// 保存设置
            SaveConfig();
        }
    }
}
复制代码

 

 

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2010-01-15 IT软件创业之 -- 曾经热血沸腾软件创业、技术带人犯下的幼稚错误总结


C# ASP.NET 通用权限设计、通用权限管理、通用权限组件、单点登录、集中式权限管理、统一授权体系、分级管理分级授权


微信扫一扫加好友



点击右上角即可分享
微信分享提示