Original source: http://www.cnblogs.com/zhaobl/archive/2011/09/29/appsettingHelper.html
1.appSettings Reading and writing
However, when you need to configure a lot of items, log all of your configuration to a separate Config.xml file, such as:
Refer to this profile in app.config:
To define the configuration single instance class AppSetting.cs management configuration information in the project:
AppSetting.cs 1 class appsetting:basesingleton<appsetting>//single Case base class
2 {
3 #region ServerIP, Servercmdport, Serverdataport
4
5///<summary>
6///Socket Server IP
7///</summary>
8 public string ServerIP;
9///<summary>
Ten///Agentserver Command Port
One///</summary>
a public int servercmdport;
///<summary>
///Agentserver Data Port
///</summary>
public int serverdataport;
17
///<summary>
19///Save configuration information
///</summary>
public void Saveserverconfig ()
22 {
this. Setconfigvalue ("ServerIP", this. ServerIP);
this. Setconfigvalue ("Servercmdport", this. Servercmdport.tostring ());
this. Setconfigvalue ("Serverdataport", this. Serverdataport.tostring ());
26}
#endregion
28
AppSetting Public ()
30 {
To this. ServerIP = this. getconfigvalue< string> ("ServerIP", "127.0.0.1");
this. Servercmdport = this. getconfigvalue< int> ("Servercmdport", 8889);
this. Serverdataport = this. getconfigvalue< int> ("Serverdataport", 8890);
this. Heartbeatintervalsec = this. getconfigvalue< int> ("Heartbeatintervalsec", 60);
35}
36
37
Private T Getconfigvalue<t> (string HashKey, T defaultvalue)
39 {
Try
41 {
Return (t) Convert.changetype (Configurationmanager.appsettings[hashkey], typeof (T));
43
44}
catch (Exception)
46 {
return defaultvalue;
48}
49}
50
Wuyi///<summary>
52///Modify the contents of the configuration item in appsettings
///</summary>
///<param name= "key" ></param>
///<param name= "value" ></param>
The public void Setconfigvalue (string key, String value)
57 {
Configuration config = configurationmanager.openexeconfiguration (configurationuserlevel.none);
The if (config. Appsettings.settings[key]!= null)
Config. Appsettings.settings[key]. Value = value;
Or else
The CONFIG. APPSETTINGS.SETTINGS.ADD (key, value);
Config. Save (configurationsavemode.modified);
Configurationmanager.refreshsection ("appSettings");
65}
66}
Read and write configuration information:
reading and writing of 2.system.servicemodel endpoints
To modify the address of a WCF Service Server service
App.config <services>
<service behaviorconfiguration= "Managerbehavior" Name= "Smyh". WinFormService.WCF.GSMService ">
<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>
<endpoint name= "Gsmserver" a ddress = "net.tcp://localhost:2011/gsmserver/" binding= "NetTcpBi nding" bindingconfiguration= "Nettcpbinding_service" contract= "smyh. WinFormService.WCF.IGSMService "/>
<baseAddresses>
<add baseaddress= "http://localhost:9999/GSMServer/"/>
</baseAddresses>
</service>
</services>
As if the default generated no Name attribute, I can't remember, if I did not add myself.
Read and modify the address value (as a service generally do not modify this path), code: read, set address 1///<summary>
2///Read EndpointAddress
3///</summary>
4///<param name= "Endpointname" ></param>
5///<returns></returns>
6 private String Getendpointaddress (String endpointname)
7 {
8 Servicessection servicessection = configurationmanager.getsection ("System.servicemodel/services") as Ser Vicessection;
9 foreach (serviceelement service in servicessection.services)
10 {
One foreach (serviceendpointelement item in service. Endpoints)
12 {
if (item. Name = = endpointname)
return item. Address.tostring ();
15}
16}
return string. Empty;
18}
19
20
///<summary>
22///Set EndpointAddress
///</summary>
///<param name= "Endpointname" ></param>
///<param name= "Address" ></param>
num private void Setendpointaddress (string endpointname, string address)
27 {
Configuration config = configurationmanager.openexeconfiguration (configurationuserlevel.none);
Servicessection clientsection = config. GetSection ("System.servicemodel/services") as servicessection;
foreach (serviceelement service in clientsection.services)
31 {
foreach (serviceendpointelement item in service. Endpoints)
33 {
if (item. Name = = endpointname)
Km &NB