Using Silverlight to read app. config or web. config

Source: Internet
Author: User

Http://blog.csdn.net/gzy11/article/details/6733417

Accidentally write a SilverlightProgramTo make it an out-of-browser running mode. Just think that if it is an out-of-browser running mode, it is a real c/s program, it should be the same as the previous winform application, can read this

Local System File. Then we started a Google tour and found a story about the mistake.ArticleSilverlight C # program: Reading and modifying the app. config file is pure nonsense. Reference with Silverlight

System. Configuration cloud. Anyone who has written the Silverlight program knows that Silverlight only supports reference of the Silverlight class library. This class library is not written for Silverlight at all, so this method does not work.

This method has many limitations and should be used with caution. The principle is to read resource files from the application package.

Original article address http://andrewtokeley.net/archive/2011/01/23/silverlight-4-ndash-simple-configuration-manager.aspx (source code see this page)

There are several main points,

The app. config file is implemented by adding an XML file to change the XML suffix name.

InternalCodeAs follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "test" value = "123456 llll"/>
</Appsettings>
</Configuration>

App. config must set the file generation option to resource mode. Otherwise, the resource file cannot be read.

The configurationmanager static class simulates the system. configuration that we usually use. You need to reference using system. xml. LINQ;

 

The configurationmanager class is as follows:

/// <Summary>
/// Access your ettings from a configuration file
/// </Summary>
/// <Remarks> your appconfig file must be in the root of your applcation </remarks>
Public static class configurationmanager
{
Static configurationmanager ()
{
Appsettings = new dictionary <string, string> ();
Readsettings ();
}
Public static dictionary <string, string> receivettings {Get; set ;}
Private Static void readsettings ()
{
// Get the name of the executing assemby-we are going to be looking in the root folder
// A file called app. config
String assemblyname = assembly. getexecutingassembly (). fullname;
Assemblyname = assemblyname. substring (0, assemblyname. indexof (','));
String url = string. Format ("{0}; component/APP. config", assemblyname );
Streamresourceinfo configfile = application. getresourcestream (New uri (URL, urikind. Relative ));
If (configfile! = NULL & configfile. stream! = NULL)
{
Stream stream = configfile. stream;
Xdocument document = xdocument. Load (Stream );
Foreach (xelement element in document. descendants ("deleetask"). descendantnodes ())
{
Deleetask. add (element. Attribute ("key"). Value, element. Attribute ("value"). value );
}
}
}
}

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.