C# 从配置文件中读取/写入信息

时间:2022-02-24 10:07:10

读取:

 var currMemberID = System.Configuration.ConfigurationManager.AppSettings["tolunaMemberID"]; //currMemberID = 8000015

Web.Config文件:

<appSettings>
<add key="tolunaMemberID" value="8000015" />
</appSettings>

写入:

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");//读取配置文件
config.AppSettings.Settings.Remove("tolunaMemberID");//移除该字段
config.AppSettings.Settings.Add("tolunaMemberID", “8000020”);//新添加字段并设置
config.Save();//保存

Web.Config文件:

<appSettings>
<add key="tolunaMemberID" value="8000020" />
</appSettings>