在C#中,可以使用System.Configuration
命名空间中的ConfigurationManager
类来读取和修改配置文件。以下是一个简单的示例代码:
string value = ConfigurationManager.AppSettings["keyName"];
Console.WriteLine("Value: " + value);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["keyName"].Value = "newValue";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
在以上示例中,keyName
是配置文件中的键,可以根据需要进行修改。记得在修改配置文件后调用Save
方法和RefreshSection
方法来保存和刷新配置文件。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:如何在C#中读取和修改配置文件