独立存储应用Using Isolated

Silverlight uses Isolated Storage as a virtual file system to store data in a hidden folder on your machine. It breaks up the data into two separate sections: Section #1 contains administrative information such as disk quota and section #2

 

Silverlight uses Isolated Storage as a virtual file system to store data in a hidden folder on your machine. It breaks up the data into two separate sections: Section #1 contains administrative information such as disk quota and section #2 contains the actual data. Each Silverlight application is allocated its own portion of the storage with the current quota set to be 1 MB per application.

Advantages:

  1. Isolated Storage is a great alterative to using cookies (as discussed in Tip of the Day #18) especially if you are working with large sets of data. Examples of use include undo functionality for your app, shopping cart items, window settings and any other setting your application can call up the next time it loads.
  2. Isolated storage stores by user allowing server applications to dedicate unique settings per individual user.

Possible Pitfalls:

  1. Administrators can set disk quota per user and assembly which means there is no guarantee on space available. For this reason, it is important to add exception handling to your code.
  2. Even though Isolated Storage is placed in a hidden folder it is possible, with a bit of effort, to find the folder. Therefore the data stored is not completely secure as users can change or remove files. It should be noted though that you can use the cryptography classes to the encrypt data stored in isolated storage preventing users from changing it.
  3. Machines can be locked down by administrative security policies preventing applications from writing to the IsolatedStorage. More specifically, code must have the IsolatedStorageFilePermission to work with isolated storage.

All that said, let’s take a look at how we save and load data. Note that you will need to add a using statement to reference the namespace System.IO.IsolatedStorage as well as 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;

        }

    }

}

 

你可能感兴趣的:(windows,Security,UP,silverlight,LINQ)