Such as:
A, increased since the node is configured in the appconfig MySettings
<? The XML version="1.0" encoding="utf-8"?>
Two, has built a management class (a prompt critical code)
Public static class MySettingsManager
{
The static IDictionary MySectionDic=(IDictionary) ConfigurationManager. GetSection (" MySettings ");
Public static string GetValue (string key)
{
[key]. Return MySectionDic ToString ();
}
///& lt; Summary>
/////modify keys
///& lt;/summary>
///& lt; Param name="key" & gt;
///& lt; Param name="value" & gt;
Public static void SetValue (string key, string value)
{
//if there is a delete
If (MySectionDic. The Contains (key)) {MySectionDic. Remove (key); }
MySectionDic. Add (key, value);
MySectionDic [key]=value;
//WaterQuSection. Save (ConfigurationSaveMode. Modified);//this is taken for granted code should be key, seek advice,
MySettings ConfigurationManager. RefreshSection (" ");
}
Public static bool KeyExists (string strKey)
{
Foreach (string STR in MySectionDic. Keys)
{
If (STR==strKey)
{
return true;
}
}
return false;
}
}
Three, Demo debugging:
Pop-up results 0 for the first time, the second pop-up results 2, that's not a problem;
But when I opened the Appconfig view, inside AveMod Value is 0, it's a problem;
Then run again, this is the first time the pop-up result is 0, the second pop-up 2 as a result, it's a problem;
The static void Main (string [] args)
{string DataBaseInfo=MySettingsManager. GetValue (" AveMod ");
MessageBox. Show (DataBaseInfo);
MySettingsManager. SetValue (" AveMod ", "2");
DataBaseInfo=MySettingsManager. GetValue (" AveMod ");
MessageBox. Show (DataBaseInfo);
}
Please understand person under the guidance, giving a way...
CodePudding user response:
Write must saveOr just write memory inside
RefreshSection just refresh from file
https://blog.csdn.net/liuhhaiffeng/article/details/52584468
CodePudding user response: