Home > Net >  App. Config section custom configuration modification
App. Config section custom configuration modification

Time:10-08

About c # App. Config custom configuration section of the article, the Internet a lot, feel oneself should know about, but encountered a problem: generally speaking, read and write access configuration section should have two parts, but I searched the Internet for half a day, the only check how to read, to how to write,

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 save
Or just write memory inside
RefreshSection just refresh from file
https://blog.csdn.net/liuhhaiffeng/article/details/52584468

CodePudding user response:

reference 1st floor stherix response:
write must save
Or just write memory inside
RefreshSection just refresh from file
https://blog.csdn.net/liuhhaiffeng/article/details/52584468

Yes, yes, all I ask is how to preserve?

WaterQuSection is IDictionary type, there is no Save method

CodePudding user response:

refer to the second floor 6 lilu9 reply:
Quote: refer to 1st floor stherix response:

Write must save
Or just write memory inside
RefreshSection just refresh from file
https://blog.csdn.net/liuhhaiffeng/article/details/52584468

Yes, yes, all I ask is how to preserve?

WaterQuSection IDictionary type, there is no Save method?


Look at the others code
To open a Configuration that can Save

CodePudding user response:

Written by XML to write out
  •  Tags:  
  • C#
  • Related