Wiki


Wiki Table of Contents

Page Details

First published by:
Last revision by:
This page has not yet been rated

Silverlight Tip of the Day #19: Using Isolated Storage

Filed under: [Edit Tags]

Silverlight sử dụng  Isolated Storage như là một hệ thống chứa file ảo để lưu trữ dữ liệu trong một thư mục ẩn trên máy của bạn.  Nó chia dữ liệu của bạn vào hai khu vực (section)  riêng biệt:  Section #1 chứa các thông tin quản trị như là dung lượng giới hạn ỗ đĩa và  section #2 chứa các dữ liệu thực . Mỗi ứng dụng  Silverlight  được cấp phần lưu trữ của chính nó  là 1MB trên một ứng dụng.

Ưu điểm:

  1. Isolated Storage là một phương pháp rất tốt để sử dụng cookies ( như đã thảo luận trong  Tip of the Day #18) đặc biệt nếu bạn làm việc với một lượng dữ liệu lớn. Những thí dụ của việc sử dụng bao gồm hủy chức năng trong ứng dụng của bạn (undo functionality for your app), shopping cart items, window settings và  bất cứ các cài đặt khác trong ứng dụng của bạn có thể được gọi trong lần load  kế tiếp .
  2. Isolated storage lưu  trữ bởi sự cho phép của ứng dụng người dùng để cài đặt cho các ứng dụng cá nhân riêng biệt  (by user allowing server applications to dedicate unique settings per individual user).

Những cạm bẫy:

  1. Những nhà quản trị  có thể đặt dung lượng giới hạn đĩa cho mỗi user và assembly , điều này có nghĩa là không có sự đảm bảo về không gian đĩa có sẵn. Vì lí do này, điều quan trọng là phải thêm phần xử lý ngoại lệ vào trong code của bạn.
  2. Mặc dù Isolated Storage được đặt trong một thư mục ẩn, có thể, với một chút nỗ lực, để tìm các thư mục. Vì vậy việc lưu trữ dữ liệu không phải là hoàn toàn an toàn như là người dùng có thể thay đổi hoặc xóa các tập tin. Cần lưu ý rằng mặc dù bạn có thể sử dụng các lớp  cryptography vào mã hoá dữ liệu lưu trữ được lưu giữ trong Isolated Storage để ngăn cản  người sử dụng thay đổi nó.
  3. Những máy tính có thể bị khóa bởi chính sách bảo mật quản trị nhằm ngăn chặn các ứng dụng thay đổi hay viết vào  IsolatedStorage. Cụ thể hơn, mã của bạn  phải có IsolatedStorageFilePermission để làm việc với lưu trữ riêng biệt.

Tất cả những gì đã nói,  hãy xem cách chúng tôi lưu và tải dữ liệu. Lưu ý rằng bạn sẽ cần phải thêm một  namespace để tham khảo là System.IO.IsolatedStorage cũng như System.IO.

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 SilverlightApplication10
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            SaveData("Hello There", "MyData.txt");
            string test = LoadData("MyData.txt");
        }
 
        private void SaveData(string data, string fileName)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
                {
                    using (StreamWriter sw = new StreamWriter(isfs))
                    {
                        sw.Write(data);
                        sw.Close();
                    }
                }
            }
        }
 
        private string LoadData(string fileName)
        {
            string data = String.Empty;
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf))
                {
                    using (StreamReader sr = new StreamReader(isfs))
                    {
                        string lineOfData = String.Empty;
                        while ((lineOfData = sr.ReadLine()) != null)
                            data += lineOfData;
                    }
                }
            }
            return data;
        }
    }
}

Thank you,
--Mike Snow

 

 

Recent Comments

Leave the first comment for this page.