Slow and steady Silverlight (16)

Source: Internet
Author: User

Introduced

Independent storage of Silverlight 2.0 data (Isolated Storage):

IsolatedStorageFile-Manipulating isolated storage classes

Isolatedstoragefile.getuserstoreforsite ()-Get the user's isolated storage by site

Isolatedstoragefile.getuserstoreforapplication ()-Get the user's isolated storage by application

Isolatedstoragesettings-Key-value dictionary table saved in isolated storage

Isolatedstoragesettings.sitesettings-Key-value Dictionary table saved by site

Isolatedstoragesettings.applicationsettings-Key-value Dictionary table saved by application

Online Demo

Http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html

Example

Isolatedstorage.xaml

<UserControl x:Class="Silverlight20.Data.IsolatedStorage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <StackPanel HorizontalAlignment="Left">
    <TextBox x:Name="txtMsg" Margin="5" />
    <TextBox x:Name="txtMsg2" Margin="5" />
    <Button x:Name="increase" Content="增加配额" Click="increase_Click" Margin="5" />
  </StackPanel>
</UserControl>

IsolatedStorage.xaml.cs

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Net;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Animation;
Using System.Windows.Shapes;
Using System.IO.IsolatedStorage;
Using System.IO;
Namespace Silverlight20.data
{
public partial class Isolatedstorage:usercontrol
{
Public IsolatedStorage ()
{
InitializeComponent ();
Demo IsolatedStorageFile
Demo ();
Demo Isolatedstoragesettings
Demo2 ();
}
/**////<summary>
Demo IsolatedStorageFile
</summary>
void Demo ()
{
Isolated Storage-Isolated storage. A virtual file system
IsolatedStorageFile-Manipulating isolated storage classes
Isolatedstoragefile.getuserstoreforsite ()-Get the user's isolated storage by site
Isolatedstoragefile.getuserstoreforapplication ()-Get the user's isolated storage by application
using (IsolatedStorageFile ISF = Isolatedstoragefile.getuserstoreforsite ())
using (IsolatedStorageFile ISF = Isolatedstoragefile.getuserstoreforapplication ())
{
DirectoryExists (PATH)-Specifies whether the paths exist
CreateDirectory (Path)-Create the specified path
FileExists (PATH)-Specifies whether the file exists
CreateFile (Path)-Create the specified file
GetDirectoryNames ()-Gets the directory array group under the root directory
GetFileNames () ()-Gets an array of file names under the root directory
GetDirectoryNames (Path)-Gets the directory array group under the specified directory
GetFileNames (Path)-Gets an array of file names under the specified directory
OpenFile ()-Opens the specified file. Refer to the documentation for specific parameters
DeleteFile (Path)-Deletes the specified file
DeleteDirectory (Path)-Deletes the specified directory (requires directory presence and no contents within the directory)
Remove ()-Closes the IsolatedStorageFile object and removes all contents from isolated storage
Create the specified directory under the root directory
if (!ISF. DirectoryExists ("Directory01"))
Isf. CreateDirectory ("Directory01");
if (!ISF. DirectoryExists ("Directory02"))
Isf. CreateDirectory ("Directory02");
Create the specified subdirectory
String subDirectory01 = System.IO.Path.Combine ("Directory01", "SubDirectory01");
String subDirectory02 = System.IO.Path.Combine ("Directory01", "SubDirectory02");
if (!ISF. DirectoryExists (SUBDIRECTORY01))
Isf. CreateDirectory (SUBDIRECTORY01);
if (!ISF. DirectoryExists (SUBDIRECTORY02))
Isf. CreateDirectory (SUBDIRECTORY02);
Create the specified file under the root directory
if (!ISF. FileExists ("RootFile.txt"))
{
IsolatedStorageFileStream ISFS = ISF. CreateFile ("RootFile01.txt");
Isfs. Close ();
}
Creates the specified file under the specified directory
String file01 = System.IO.Path.Combine (subDirectory01, "File01.txt");
String file02 = System.IO.Path.Combine (subDirectory01, "File02.txt");
String file03 = System.IO.Path.Combine (subDirectory01, "file03.xml");
if (!ISF. FileExists (FILE01))
{
IsolatedStorageFileStream-stream of files within isolated storage. Inherit from FileStream
IsolatedStorageFileStream ISFS = ISF. CreateFile (FILE01);
Isfs. Close ();
}
if (!ISF. FileExists (FILE02))
{
IsolatedStorageFileStream ISFS = ISF. CreateFile (FILE02);
Isfs. Close ();
}
if (!ISF. FileExists (FILE03))
{
IsolatedStorageFileStream ISFS = ISF. CreateFile (FILE03);
Isfs. Close ();
}
Txtmsg.text + = "Directory list under root directory: \ r \ n";
Gets the directory array group under the root directory
foreach (String directoryname in ISF. GetDirectoryNames ())
{
Txtmsg.text + = directoryname + "\ r \ n";
}
Txtmsg.text + = "file list under root directory: \ r \ n";
Gets an array of file names under the root directory
foreach (String FileName in ISF.) GetFileNames ())
{
Txtmsg.text + = FileName + "\ r \ n";
}
Txtmsg.text + = "directory Directory01 list: \ r \ n";
Get the directory array group in the DIRECTORY01 directory
foreach (String directoryname in ISF. GetDirectoryNames (SUBDIRECTORY01))
{
Txtmsg.text + = directoryname + "\ r \ n";
}
Txtmsg.text + = "Directory directory01/subdirectory01 *.txt file list: \ r \ n";
Get an array of file names in the Directory01/subdirectory01 directory with a suffix named txt
foreach (String FileName in ISF.) GetFileNames (System.IO.Path.Combine (subDirectory01, "*.txt"))
{
Txtmsg.text + = FileName + "\ r \ n";
}
if (ISF. FileExists (FILE01))
{
Writing content in File file01
IsolatedStorageFileStream streamwrite = ISF. OpenFile (FILE01, FileMode.Open, FileAccess.Write);
using (StreamWriter sw = new StreamWriter (streamwrite))
{
Sw. WriteLine ("I am: Webabcd");
Sw. WriteLine ("I focus on asp.net, Silverlight");
}
Txtmsg.text + = "file File01.txt content: \ r \ n";
Read the contents of the file file01
IsolatedStorageFileStream streamread = ISF. OpenFile (FILE01, FileMode.Open, FileAccess.Read);
using (StreamReader sr = new StreamReader (streamread))
{
Txtmsg.text = Sr. ReadToEnd ();
}
}
Delete File file01
if (ISF. FileExists (FILE01))
{
Isf. DeleteFile (FILE01);
}
Try
{
Delete directory SubDirectory01
Isf. DeleteDirectory (SUBDIRECTORY01);
}
catch (IsolatedStorageException ex)
{
IsolatedStorageException-Exception thrown when operation temporary storage fails
Because there are files in the SubDirectory01 directory, the exception is thrown
Txtmsg.text = ex. message;
}
}
}
/**////<summary>
Demo Isolatedstoragesettings
</summary>
void Demo2 ()
{
Isolatedstoragesettings-Key-value dictionary table saved in isolated storage
Isolatedstoragesettings.sitesettings-Key-value Dictionary table saved by site
Isolatedstoragesettings.applicationsettings-Key-value Dictionary table saved by application
isolatedstoragesettings ISS = isolatedstoragesettings.applicationsettings;
Add (key, value)-adding a pair of key-value
Iss. ADD ("Key", "value");
Txtmsg2.text + = (string) iss["key" + "\ r \ n";
Modifies the value of the specified key
iss["key"] = "value2";
Txtmsg2.text + = (string) iss["key" + "\ r \ n";
Remove (key)-Removes the specified key
Count-The total number of key-value in the dictionary table
Iss. Remove ("key");
Txtmsg2.text = Iss. Count;
}
private void Increase_click (object sender, RoutedEventArgs e)
{
To demonstrate a related operation for quotas for isolated storage
using (IsolatedStorageFile ISF = Isolatedstoragefile.getuserstoreforapplication ())
{
Quota-Current quota (KB)
Increasequotato (newquotasize)-increase to the specified quota
Availablefreespace-Current available quotas
Isf. Increasequotato (ISF. Quota + 1 * 1024 * 1024);
System.Windows.Browser.HtmlPage.Window.Alert (
String. Format ("current quota: {0}; Available quotas: {1}", ISF.) Quota, ISF. Availablefreespace));
}
}
}
}

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.