现在的位置: 首页 > 综合 > 正文

SilverLight学习笔记–本地(客户端)数据存储

2013年05月12日 ⁄ 综合 ⁄ 共 6596字 ⁄ 字号 评论关闭

本地(客户端)数据存储

通过使用独立存储,数据将始终按用户在虚拟文件系统中隔离,虚拟文件系统可以是根目录中的一个文件,也可以是一个目录和文件树。

独立存储数据舱是一个抽象的存储位置,而不是一个具体的存储位置。它由一个或多个独立的存储文件(称为存储区)组成,这些存储文件包含存储数据的实际目录位置。任何类型的数据都可以保存到存储区中。

存储区通常驻留在客户端,但应用程序也可以使用服务器上的独立存储。

SDK没有说文件存储的地方,搜索一下发现存在于类似以下目录的地方:

C:"Documents and Settings"Administrator.WANGXIN.000"Local Settings"Application Data"Microsoft"Silverlight"is"fonkhtyt.ues"gdnfncxm.pga"1"s"yqvlp0zp0vt53mbuwhvhmrf0h2a0a0viyspfl24d0iwrtdzxevaaafba"f

本地文件操作

public static void Demo(TextBlock outputBlock)

        {

// 获取本地存储设置

try

            {

using (var store = IsolatedStorageFile.GetUserStoreForApplication())

                {

// 使用StringBuilder构造结构.

StringBuilder sb = new StringBuilder();

// 建三个目录.

                    store.CreateDirectory("MyApp1");

                    store.CreateDirectory("MyApp2");

                    store.CreateDirectory("MyApp3");

// 在MyApp1目录下建三个子目录.

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

// 建新文件.

IsolatedStorageFileStream rootFile = store.CreateFile("InTheRoot.txt");

                    rootFile.Close();

// 子目录下建新文件.

IsolatedStorageFileStream subDirFile =

                        store.CreateFile(Path.Combine(subdirectory1, "MyApp1A.txt"));

                    subDirFile.Close();

// 根目录

string[] directoriesInTheRoot = store.GetDirectoryNames();

string[] filesInTheRoot = store.GetFileNames();

string searchpath = Path.Combine(subdirectory1, "*.*");

string[] filesInSubDirs = store.GetFileNames(searchpath);

// '*' 搜索子目录下文件

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

// 写文件

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

                        }

                    }

// 读文件

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

                    }

// 删文件.

try

                    {

if (store.FileExists(filePath))

                        {

//store.DeleteFile(filePath);

                        }

                    }

catch (IsolatedStorageException ex)

                    {

                        sb.AppendLine(ex.Message);

                    }

// 删目录

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.

            }

        }

    }

本地键值字典

下面的示例创建一个使用独立存储的应用程序设置字典,添加键/值对,检索该值,更改该值,然后再删除该值。在 Windows Vista 中,该信息存储在 AppData"LocalLow 目录中。对于其他操作系统版本(包括 Apple Macintosh 上的操作系统)而言,该信息存储在 AppData"Local 目录中。

<StackPanel Height="230" Margin="10" HorizontalAlignment="Left" VerticalAlignment="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>

打开 Page.xaml.cs

private IsolatedStorageSettings appSettings = 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)appSettings["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)appSettings["email"];

        }

private void bDelete_Click(object sender, RoutedEventArgs e)

        {

            appSettings.Remove("email");

            tbResults.Text = "Email deleted. Click Retrieve to confirm deletion.";

        }

抱歉!评论已关闭.