Silverlight C # program: Read and modify the app. config file

Source: Internet
Author: User

1. Add the app. config file to the project:

Right-click the project name and select "add"> "Add new project". In the displayed "Add new project" dialog box, select "add application ".ProgramConfiguration file. If there is no configuration file in the project before, the default file name is "app. config" and click "OK ". The app. config file that appears in the designer view is:

 

 
<?Xmlversion = "1.0" encoding = "UTF-8"?>
<Configuration>
</Configuration>

Note: after the project is compiled, Run "bin \ users. config ". The first file is the configuration file actually used by the project. All changes made during the program running will be saved here. The second file is the originalCodeThe synchronization file of "app. config" will not be changed during the program running.

2. connectionstrings configuration section:

NOTE: If your SQL version is 2005 Express, the name of the SQL server instance is localhost \ sqlexpress by default. You must change "Data Source = localhost; "is" Data Source = localhost \ sqlexpress; ". Do not add spaces on both sides of the equal sign.

 

 <! --  Database connection string  -->  
< Connectionstrings >
< Clear />
< Addname = "Conjxcbook"
Connectionstring = "Data Source = localhost; initial catalog = jxcbook; user id = sa; Password = ********"
Providername = "System. Data. sqlclient" />
</ Connectionstrings >

3. appsettings configuration section:

The appsettings configuration section is the configuration of the entire program. If you configure the current user, use the usersettings configuration section. The format is the same as the following configuration writing requirements.

 

 <! --  Parameters required for invoicing management system initialization  -->  
< Appsettings >
< Clear />
< Addkey = "Username" Value = "" />
< Addkey = "Password" Value = "" />
< Addkey = "Department" Value = "" />
< Addkey = "Returnvalue" Value = "" />
< Addkey = "Pwdpattern" Value = "" />
< Addkey = "Userpattern" Value = "" />
</ Appsettings >

4. Read and Update app. config

For the read and write operations on the app. config file, refer to the networkArticle: Http://www.codeproject.com/csharp/ systemconfiguration. asp is titled "read/write app. config file with. NET 2.0.

Note: To use the following code to access the app. config file, in addition to adding a reference to system. configuration, you must also add a reference to system. configuration. dll in the project.

4.1 read connectionstrings configuration section

 

 ///  <Summary>  
/// Returns the data connection string based on the connection string name connectionname
/// </Summary>
/// <Param name = "connectionname"> </param>
/// <Returns> </returns>
Private Static String Getconnectionstringsconfig ( String Connectionname)
{
String Connectionstring =
Configurationmanager. connectionstrings [connectionname]. connectionstring. tostring ();
Console. writeline (connectionstring );
Return Connectionstring;
}

4.2 connectionstrings configuration update

 

 ///  <Summary> 
/// Update connection string
/// </Summary>
/// <Param name = "newname"> Connection string name </Param>
/// <Param name = "newconstring"> Connection string content </Param>
/// <Param name = "newprovidername"> Data provider name </Param>
Private Static Void Updateconnectionstringsconfig ( String Newname,
String Newconstring,
String Newprovidername)
{
Bool Ismodified = False ; // Record whether the connection string already exists
// If the connection string to be modified already exists
If (Configurationmanager. connectionstrings [newname] ! = Null )
{
Ismodified = True ;
}
// Create a connection string instance
Connectionstringsettings mysettings =
New Connectionstringsettings (newname, newconstring, newprovidername );
// Open the executable configuration file *. EXE. config
Configuration config =
Configurationmanager. openexeconfiguration (configurationuserlevel. None );
// If the connection string already exists, delete it first.
If (Ismodified)
{
Config. connectionstrings. connectionstrings. Remove (newname );
}
// Add a new connection string to the configuration file.
Config. connectionstrings. connectionstrings. Add (mysettings );
// Save changes to the configuration file
Config. Save (configurationsavemode. modified );
// Force reload the connectionstrings configuration section of the configuration file
Configurationmanager. refreshsection ( " Connectionstrings " );
}

4.3 read the appstrings configuration section

 

 ///  <Summary>  
/// Return the value in the configuration section of ettings in *. EXE. config.
/// </Summary>
/// <Param name = "strkey"> </param>
/// <Returns> </returns>
Private Static String Getappconfig ( String Strkey)
{
Foreach ( String Key In Configurationmanager. configurettings)
{
If (Key = Strkey)
{
Return Configurationmanager. configurettings [strkey];
}
}
Return Null ;
}

4.4 connectionstrings configuration update

 

 ///  <Summary>  
/// In the *. EXE. config file, add a pair of key and value pairs to the deleettings configuration section.
/// </Summary>
/// <Param name = "newkey"> </param>
/// <Param name = "newvalue"> </param>
Private Static Void Updateappconfig ( String Newkey, String Newvalue)
{
Bool Ismodified = False ;
Foreach ( String Key In Configurationmanager. configurettings)
{
If (Key = Newkey)
{
Ismodified = True ;
}
}

// Open app. config of executable
Configuration config =
Configurationmanager. openexeconfiguration (configurationuserlevel. None );
// You need to remove the old settings object before you can replace it
If (Ismodified)
{
Config. Fig. settings. Remove (newkey );
}
// Add an application setting.
Config. appsettings. settings. Add (newkey, newvalue );
// Save the changes in APP. config file.
Config. Save (configurationsavemode. modified );
// Force a reload of a changed section.
Configurationmanager. refreshsection ( " Appsettings " );
}

author: memories of lost youth
Source: http://www.cnblogs.com/lukun/
copyright of this Article It is shared by the author and the blog site. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection clearly on the article page. If there is any problem, thank you very much for contacting me via http://www.cnblogs.com/lukun/ .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.