C# 读写app.config文件

时间:2022-01-05 11:24:48

使用ConfigurationManager读取app.config的内容。

添加“System.Configuration”

using System;
using System.Configuration;
using System.IO;
using System.Windows.Forms;
namespace KEEN_EYES
{
/// <summary>
/// 目录FORM
/// </summary>
public partial class Folder_Form
{

/// <summary>
/// 构造方法
/// </summary>
public Folder_Form()
{
InitializeComponent();
}

/// <summary>
/// 得到app.config值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private string getKeyValue(string key)
{
return ConfigurationManager.AppSettings[key];
}

/// <summary>
/// 设置app.config值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
private void setKeyValue(string key, string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings[key].Value = value;
config.Save(ConfigurationSaveMode.Modified);
}
}
}