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

简介:

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

 

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

 

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

 

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

// ------------------------------------------------------------
//  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();
        }
    }
}

 

本文转自jirigala_bao 51CTO博客,原文链接:http://blog.51cto.com/jirigala/809495


相关文章
|
数据可视化 小程序 程序员
uni-app 配置编译环境与动态修改manifest.json参数
uni-app 配置编译环境与动态修改manifest.json参数
1450 0
|
5月前
|
数据库连接 C# 数据库
C#数据库连接配置文件存放至App.Config
将C#数据库连接配置文件存放到外置的App.config文件中,并且演示vs和Rider如何读取配置文件连接数据库
105 0
|
6月前
|
人工智能 自然语言处理 小程序
微信小程序打开项目提示读取project.config.json文件失败
微信小程序打开项目提示读取project.config.json文件失败
78 0
|
C#
WPF C#之读取并修改App.config文件
原文:WPF C#之读取并修改App.config文件 简单介绍App.config App.config文件一般是存放数据库连接字符串的。
2773 0
|
PHP
YII2 配置gii之后页面404 解决 2点=1 要加载model,2 要设置环境为dev,如下截图 3次要---有时候可能需要 执行composer dump-autoload 重新加载类
YII2 配置gii之后页面404  解决 2点=1 要加载model,2 要设置环境为dev,如下截图 解决成功     我的是这么解决的
1136 0