C# 配置文件相关方法
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
class Program
{
static void Main()
{
// 获取应用程序的根目录路径
string appDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configFilePath = Path.Combine(appDirectory, "reg.config");
// 调用方法来创建配置文件
CreateConfigFile(configFilePath);
// 键值对字典
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>
{
{ "key1", "zwNo3bHIOs94h8Gnw0qFoZ6GqX4W0/6AN6soH1gjOjQ=" },
{ "key2", "another_value" },
{ "key3", "yet_another_value" }
};
// 写入多个键值对
WriteKeyValuePairs(configFilePath, keyValuePairs);
// 更新其中一个键值对
UpdateKeyValuePair(configFilePath, "key2", "new_value_for_key2");
}
// 创建配置文件
static void CreateConfigFile(string filePath)
{
if (!File.Exists(filePath))
{
// 创建一个新的配置文件
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.SaveAs(filePath);
Console.WriteLine("配置文件已创建: " + filePath);
}
else
{
Console.WriteLine("配置文件已存在.");
}
}
// 写入多个键值对
static void WriteKeyValuePairs(string filePath, Dictionary<string, string> keyValuePairs)
{
try
{
// 打开指定的配置文件
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = filePath }, ConfigurationUserLevel.None);
foreach (var pair in keyValuePairs)
{
if (config.AppSettings.Settings[pair.Key] == null)
{
config.AppSettings.Settings.Add(pair.Key, pair.Value);
}
else
{
config.AppSettings.Settings[pair.Key].Value = pair.Value;
}
}
config.Save();
Console.WriteLine("键值对已写入.");
}
catch (Exception ex)
{
Console.WriteLine("写入键值对时出错: " + ex.Message);
}
}
// 更新键值对
static void UpdateKeyValuePair(string filePath, string key, string newValue)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] != null)
{
config.AppSettings.Settings[key].Value = newValue;
config.Save();
Console.WriteLine("键值对已更新: " + key + " = " + newValue);
}
else
{
Console.WriteLine("键不存在,无法更新.");
}
}
catch (Exception ex)
{
Console.WriteLine("更新键值对时出错: " + ex.Message);
}
}
}
配合使用:
// 获取当前工作目录
string currentWorkingDirectory = Directory.GetCurrentDirectory();
// 定义文件名
string fileName = "reg.config";
// 组合路径和文件名
string configFilePath = Path.Combine(currentWorkingDirectory, fileName);
//注册信息写入配置文件
Public.CreateConfigFile(configFilePath);
读取配置文件:
// 映射配置文件
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
// 读取键值
string keyValue = null;
if (config.AppSettings.Settings["key"] != null)
{
keyValue = config.AppSettings.Settings["key"].Value;
Console.WriteLine($"键 'key' 的值为: {keyValue}");
}
else
{
Console.WriteLine("键 'key' 不存在");
}
分类:
C# / WinForm
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!