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 );
}
}
}
}