Silverlight learning notes-local (client) Data Storage

Source: Internet
Author: User

Local (client) Data Storage

By using independent storage, data is always isolated by users in the Virtual File System. The virtual file system can be a file in the root directory or a directory and a file tree.

The independent storage data compartment is an abstract storage location, rather than a specific storage location. It consists of one or more independent storage files (called buckets) that contain the actual directory location of the stored data. Any type of data can be stored in the storage area.

The storage area usually resides on the client, but the applicationProgramYou can also use independent storage on the server.

The SDK does not describe the location where the file is stored. Search for a location found to exist in a directory similar to the following:

C: "Documents and Settings" Administrator. wangxin.000 "Local Settings" Application Data "Microsoft" Silverlight "is" fonkhtyt. ues "gdnfncxm. PGA" 1 "S" releases "F

Local file operations

Public static void demo (textblock outputblock)

{

// Get local storage settings

Try

{

Using (VAR store = isolatedstoragefile. getuserstoreforapplication ())

{

// Use stringbuilder to construct the structure.

Stringbuilder sb = new stringbuilder ();

// Create three directories.

Store. createdirectory ("myapp1 ");

Store. createdirectory ("myapp2 ");

Store. createdirectory ("myapp3 ");

// Create three subdirectories under the myapp1 directory.

String subdirectory1 = path. Combine ("myapp1", "subdir1 ");

String subdirectory2 = path. Combine ("myapp1", "subdir2 ");

String subdirectory3 = path. Combine ("myapp1", "subdir3 ");

Store. createdirectory (subdirectory1 );

Store. createdirectory (subdirectory2 );

Store. createdirectory (subdirectory3 );

// Create a new file.

Isolatedstoragefilestream rootfile = store. createfile ("intheroot.txt ");

Rootfile. Close ();

// Create a file in the subdirectory.

Isolatedstoragefilestream subdirfile =

Store. createfile (path. Combine (subdirectory1, "myapp1a.txt "));

Subdirfile. Close ();

// Root directory

String [] directoriesintheroot = store. getdirectorynames ();

String [] filesintheroot = store. getfilenames ();

String searchpath = path. Combine (subdirectory1 ,"*.*");

String [] filesinsubdirs = store. getfilenames (searchpath );

// '*' Search for files in the subdirectory

String [] subdirectories =

Store. getdirectorynames (path. Combine ("myapp1 ","*"));

SB. appendline ("directories in root :");

Foreach (string dir in directoriesintheroot)

{

SB. appendline ("-" + DIR );

}

SB. appendline ();

SB. appendline ("directories under myapp1 :");

Foreach (string sdir in subdirectories)

{

SB. appendline ("-" + sdir );

}

SB. appendline ();

SB. appendline ("files in the root :");

Foreach (string filename in filesintheroot)

{

SB. appendline ("-" + filename );

}

SB. appendline ();

// List files in myapp1 "subdir1.

SB. appendline (@ "files in myapp1" subdir1 :");

Foreach (string filename in filesinsubdirs)

{

SB. appendline ("-" + filename );

}

SB. appendline ();

// Write an object

String filepath = path. Combine (subdirectory1, "myapp1a.txt ");

If (store. fileexists (filepath ))

{

Try

{

Using (streamwriter Sw =

New streamwriter (store. openfile (filepath,

Filemode. Open, fileaccess. Write )))

{

Sw. writeline ("to do list :");

Sw. writeline ("1. buy supplies .");

}

}

Catch (isolatedstorageexception ex)

{

SB. appendline (ex. Message );

}

}

// Read the file

Try

{

Using (streamreader reader =

New streamreader (store. openfile (filepath,

Filemode. Open, fileaccess. Read )))

{

String contents = reader. readtoend ();

SB. appendline (filepath + "Contents :");

SB. appendline (contents );

}

}

Catch (isolatedstorageexception ex)

{

SB. appendline (ex. Message );

}

// Delete an object.

Try

{

If (store. fileexists (filepath ))

{

// Store. deletefile (filepath );

}

}

Catch (isolatedstorageexception ex)

{

SB. appendline (ex. Message );

}

// Delete the Directory

String dirdelete = path. Combine ("myapp1", "subdir3 ");

Try

{

If (store. directoryexists (dirdelete ))

{

Store. deletedirectory (dirdelete );

}

}

Catch (isolatedstorageexception ex)

{

SB. appendline (ex. Message );

}

SB. appendline ();

// Remove the store

Store. Remove ();

SB. appendline ("store removed .");

Outputblock. Text = sb. tostring ();

}

}

Catch (isolatedstorageexception)

{

// Todo: handle that store was unable to be accessed.

}

}

}

Local key-value dictionary

The following example creates an application that uses the independent storage to set the dictionary, add a key/value pair, retrieve the value, change the value, and then delete the value. In Windows Vista, this information is stored in the appdata "locallow directory. For other operating system versions (including those on the Apple Macintosh), this information is stored in the appdata "local directory.

<Stackpanel Height = "230" margin = "10" horizontalalignment = "Left" verticalignment = "TOP">

<Button Height = "30" width = "60" X: Name = "BADD" content = "add" margin = "10"

Click = "badd_click"> </button>

<Button Height = "30" width = "60" X: Name = "bretrieve" content = "retrieve" margin = "10"

Click = "bretrieve_click"> </button>

<Button Height = "30" width = "60" X: Name = "bchange" content = "change" margin = "10"

Click = "bchange_click"> </button>

<Button Height = "30" width = "60" X: Name = "bdelete" content = "delete" margin = "10"

Click = "bdelete_click"> </button>

</Stackpanel>

<Textbox Height = "50" width = "280" background = "beige" X: Name = "tbresults"

Borderbrush = "beige" borderthickness = "5"

Fontfamily = "Arial" fontsize = "12" text = "Silverlight test area"

Horizontalalignment = "right"> </textbox>

Open page. XAML. CS

Private isolatedstoragesetemediacettings = isolatedstoragesettings. applicationsettings;

Private void badd_click (Object sender, routedeventargs E)

{

Try

{

Appsettings. Add ("email", "someone@contoso.com ");

Tbresults. Text = "Settings stored .";

}

Catch (argumentexception ex)

{

Tbresults. Text = ex. message;

}

}

Private void bretrieve_click (Object sender, routedeventargs E)

{

Try

{

Tbresults. Text = "setting retrieved:" + (string) receivettings ["email"];

}

Catch (system. Collections. Generic. keynotfoundexception ex)

{

Tbresults. Text = ex. message;

}

}

Private void bchange_click (Object sender, routedeventargs E)

{

Appsettings ["email"] = "someone@fabrikam.com ";

Tbresults. Text = "changed to:" + (string) receivettings ["email"];

}

Private void bdelete_click (Object sender, routedeventargs E)

{

Deleetask. remove ("email ");

Tbresults. Text = "email deleted. Click retrieve to confirm deletion .";

}

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.