c#读写配置文件
读配置很简单,可以用ConfigurationManager.AppSettings[key] 来读出,
可是写配置文件时,如果写成这样
ConfigurationManager.AppSettings[key] = "111";
总是提示只读,那么该怎么办呢?
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Configuration;
- namespace BQKJ.Common
- {
- /// <summary>
- /// 对exe.Config文件中的appSettings段进行读写配置操作
- /// 注意:调试时,写操作将写在vhost.exe.config文件中
- /// </summary>
- public class ConfigAppSettings
- {
- /// <summary>
- /// 写入值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void SetValue(string key, string value)
- {
- //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
- System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (config.AppSettings.Settings[key] == null)
- {
- config.AppSettings.Settings.Add(key, value);
- }
- else
- {
- config.AppSettings.Settings[key].Value = value;
- }
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
- }
- /// <summary>
- /// 读取指定key的值
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string GetValue(string key)
- {
- System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (config.AppSettings.Settings[key] == null)
- return "";
- else
- return config.AppSettings.Settings[key].Value;
- }
- }
- }
其实也很简单,用这两个封装过的方法就可以了。
需要注意的是,在IDE调试时,写入的配置文件其实是写在了.vshost.exe.config文件中,所以你在.exe.config中是看不到的。只有直接运行exe文件时,才会正确写入到.exe.config中。
private void button1_Click(object sender, EventArgs e)
{
string serverIP = ConfigHelper.GetValue("ServerIP");
string db = ConfigHelper.GetValue("DataBase");
string user = ConfigHelper.GetValue("user");
string password = ConfigHelper.GetValue("password");
string info = "serverIP:" + serverIP + "\r\n"
+ "db:" + db + "\r\n"
+ "user:" + user + "\r\n"
+ "password:" + password + "\r\n";
MessageBox.Show(info);
ConfigHelper.SetValue("DataBase", "ttdb");
string newIP = ConfigHelper.GetValue("DataBase");
MessageBox.Show(newIP);
}
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Configuration;
- namespace BQKJ.Common
- {
- /// <summary>
- /// 对exe.Config文件中的appSettings段进行读写配置操作
- /// 注意:调试时,写操作将写在vhost.exe.config文件中
- /// </summary>
- public class ConfigAppSettings
- {
- /// <summary>
- /// 写入值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void SetValue(string key, string value)
- {
- //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
- System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (config.AppSettings.Settings[key] == null)
- {
- config.AppSettings.Settings.Add(key, value);
- }
- else
- {
- config.AppSettings.Settings[key].Value = value;
- }
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
- }
- /// <summary>
- /// 读取指定key的值
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string GetValue(string key)
- {
- System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (config.AppSettings.Settings[key] == null)
- return "";
- else
- return config.AppSettings.Settings[key].Value;
- }
- }
- }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤