Independent storage zone with Silverlight features-isolatedstorage

Source: Internet
Author: User

isolatedstorage is a special feature of Silverlight. It is a virtual file system of Silverlight. It is called a virtual file system, the official Microsoft documentation explains that the root of the Virtual File System is located in the fuzzy processing folder of each user on the physical file system. Each unique identifier provided by the host is mapped to a different root, this root provides its own Virtual File System for each application Program . An application cannot navigate from its own file system to another file system. That is to say, apart from some highly trusted Code and management tools that can be accessed from other assemblies, as well as unmanaged code that can be accessed, this space can only be accessed by this program, and thus it is highly secure. Windows Phone 7 is based on Silverlight, and its file system is also isolatedstorage. Its default size is 2 GB. Because of its security, it is not suitable for storing big data and important data, suitable for saving temporary data.

The namespace of isolatedstorage isSystem. Io. isolatedstorage, Which is included inIn the mscorlib assembly, there are four classes in the namespace:Isolatedstoragefile, isolatedstoragefilestream,Isolatedstorageexception,Isolatedstoragesettings, WhereIsolatedstoragefile and isolatedstoragefilestreamFor common use, the following is a simple example using these two classes.

To write and read files in isolatedstorage, first create a project and write the XAML code in mainpage. XAML:

 < Phone: phoneapplicationpage
X: Class = "Demo. mainpage"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: Phone = "CLR-namespace: Microsoft. Phone. controls; Assembly = Microsoft. Phone"
Xmlns: Shell = "CLR-namespace: Microsoft. Phone. Shell; Assembly = Microsoft. Phone"
Xmlns: d = "Http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006"
MC: ignorable = "D" D: designwidth = "480" D: designheight = "696"
Fontfamily =" {Staticresource phonefontfamilynormal} "
Fontsize =" {Staticresource phonefontsizenormal} "
Foreground =" {Staticresource phoneforegroundbrush} "
Supportedorientations = "Portraitorlandscape" Orientation = "Portrait"
Shell: systemtray. isvisible = "True" >

<! -- Layoutroot is the root grid where all page content is placed -->
< Grid X: Name = "Layoutroot" Background = "Transparent" >
< Grid. rowdefinitions >
< Rowdefinition Height = "Auto" />
< Rowdefinition Height = "*" />
</ Grid. rowdefinitions >

<! -- Titlepanel contains the name of the application and page title -->
< Stackpanel X: Name = "Titlepanel" Grid. Row = "0" Margin =" >
< Textblock X: Name = "Applicationtitle" Text = "My application" Style =" {Staticresource phonetextnormalstyle} " />
< Textblock X: Name = "Pagetitle" Text = "Page name" Margin = "9,-7, 0, 0" Style =" {Staticresource phonetexttitle1style} " />
</ Stackpanel >

<! -- Contentpanel-place additional content here -->
< Grid X: Name = "Contentpanel" Grid. Row = "1" Margin = "12, 0, 12, 0" >
< Button Content = "Writing Files" Height = "72" Horizontalalignment = "Left" Margin = "9,148" Name = "Button2" Verticalalignment = "TOP" Width = "160" Click = "Button2_click" />
< Button Content = "Reading files" Height = "72" Horizontalalignment = "Left" Margin = "205,148" Name = "Button3" Verticalalignment = "TOP" Width = "160" Click = "Button3_click" />
< Textbox Height = "72" Horizontalalignment = "Left" Margin =" Name = "Textbox1" Text = "" Verticalalignment = "TOP" Width = "460" />
< Textblock Height = "30" Horizontalalignment = "Left" Margin = "6,226" Name = "Textblock1" Text = "" Verticalalignment = "TOP" />
</ Grid >
</ Grid >

<! -- Sample Code showing usage of ApplicationBar -->
<! -- <Phone: phoneapplicationpage. ApplicationBar>
<Shell: ApplicationBar isvisible = "true" ismenuenabled = "true">
<Shell: applicationbariconbutton iconuri = "/images/appbar_button1.png" text = "button 1"/>
<Shell: applicationbariconbutton iconuri = "/images/appbar_button2.png" text = "button 2"/>
<Shell: ApplicationBar. menuitems>
<Shell: applicationbarmenuitem text = "menuitem 1"/>
<Shell: applicationbarmenuitem text = "menuitem 2"/>
</Shell: ApplicationBar. menuitems>
</Shell: ApplicationBar>
</Phone: phoneapplicationpage. ApplicationBar> -->

</ Phone: phoneapplicationpage >

The mainpage class defines an independent storage object, which corresponds to a specific independent storage range and is initialized in the constructor.

 
Isolatedstoragefile ISF;
PublicMainpage ()
{
Initializecomponent ();
//Get application space, initialize
ISF=Isolatedstoragefile. getuserstoreforapplication ();
}

Last useIsolatedstoragefilestream class,Add events for the "Write File" and "Read File" buttons.

C # code:

    Private     Void  Button2_click (  Object  Sender, routedeventargs E)
{
// File write operations
Isolatedstoragefilestream filestream = New Isolatedstoragefilestream ( " Text.txt " , Filemode. Create, fileaccess. Write, ISF );
Streamwriter = New Streamwriter (filestream );
If ( This . Textbox1.text ! = "" )
{
Writer. writeline ( This . Textbox1.text );
Writer. Close ();
}
Else
{
MessageBox. Show ( " The file to be written cannot be blank! " );
}
}

Private Void Button3_click ( Object Sender, routedeventargs E)
{
// File Read operations
Isolatedstoragefilestream filestream = New Isolatedstoragefilestream ( " Text.txt " , Filemode. Open, fileaccess. Read, ISF );
Streamreader = New Streamreader (filestream );
This . Textblock1.text = Reader. readtoend ();
Reader. Close ();
}

All right, a simple file can be written and read.

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.