Silverlight 2.0小技巧(8) 讀寫Isolated Storage

摘要:Silverlight 2.0小技巧(8) 讀寫Isolated Storage

Silverlight允許應用將資料存放客戶端(「Isolated Storage」),可讓User自己設定該儲存體空間的大小(例如,USer也許會准許一個Silverlight 程式擁有50MB的儲存空間)


//寫入IsolatedStorage
private void SaveIsolatedStorage(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();
                    }
                }
            }
        }
//讀取IsolatedStorage
private string LoadIsolatedStorage(string fileName)
        {
            string strFileData = 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)
                            strFileData += lineOfData;
                    }
                }
            }
            return strFileData;
        }
註:如果重複寫入同一個檔,不會發生錯誤..會覆蓋過去