WP7 独立存储

大致分为三类 : settings , files/folders, relation data

AP_Con_IStorage3

MSD关于线程同步的建议:

To synchronize access to isolated storage between different threads, we recommend using the System.Threading.Mutex class. A mutex grants exclusive access to a shared resource to only one thread. If one thread acquires a mutex, the second thread that wants to acquire that mutex is suspended until the first thread releases the mutex. For example, a mutex could be used to synchronize access to an isolated storage folder between a Windows Phone application running in the foreground and a background agent.

1. 特殊用处的文件夹:

除了用户自定义创建的系统还创建了一些用于特殊目的的文件夹:‘

Path

Use

Notes

Shared

Used to host the MediaShellContent, and Transfers folders.

This folder is created when your application is installed, but it can be deleted.

Shared/Media

Album Art: Applications can use this folder to display album art in the Universal Volume Control (UVC) while playing background audio.

This folder is created when your application is installed, but it can be deleted. For more information, see How to: Integrate with the Music and Videos Hub for Windows Phone.

Shared/ShellContent

Application Tiles: Tiles can display background images on the front and back of each Tile. Background images can be stored in isolated storage, but must be located in this folder or a subfolder of it.

This folder is created when your application is installed, but it can be deleted. For more information, see Tiles Overview for Windows Phone.

Shared/Transfers

Background file transfer: Applications can save files to isolated storage in the background, even when the application is no longer running in the foreground.

This folder is created when your application is installed, but it can be deleted. For more information, see Background File Transfers Overview for Windows Phone.

2.查看独立存储的内容

   2. 1 微软SDK提供的:Use Isolated Storage Explorer (ISETool.exe), to list, copy, and replace files and directories in isolated storage in a Windows Phone application. Isolated Storage Explorer is a command-line tool that gets installed with Windows Phone SDK 7.1. This tool enables you to verify that files are being saved in the correct location with the correct data. Isolated Storage Explorer can be used in the emulator or a device. For more information, see How to: Use the Isolated Storage Explorer Tool.

  2.2 一些用户自己开发的可视化工具

   http://blogs.msdn.com/b/mikeormond/archive/2011/09/01/free-gui-isolated-storage-explorer-tool.aspx

3. 具体代码

3.1创建设置界面

IsolatedStorageSettings Class

http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragesettings(VS.95).aspx

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;



settings.Add(Key, value);

settings[Key] = value;



 if (settings.Contains(Key))

 {

        value = (T)settings[Key];

 }

需要注意的是数据到独立存储并不是立即执行的,MSDN:

Data written to the IsolatedStorageSettings object is saved when the application that uses the class is closed. This can occur when the user closes the Web browser, refreshes a page, or browses away from the page. If you want your application to write to isolated storage immediately, you can call the Save method in application code.

If the IsolatedStorageFile does not have enough available free space to save your application settings, an IsolatedStorageException will be thrown. If more space is required, use the IsolatedStorageFile.IncreaseQuotaTo method to request more storage space from the host.

2.文件和文件夹

// 创建一个文件夹,并创建一个myFile.txt,写入一段文字。

    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();



    // Create a new folder and call it "MyFolder".

    myStore.CreateDirectory("MyFolder");



    // Specify the file path and options.

    using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.OpenOrCreate, myStore))

    {

        //Write the data

        using (var isoFileWriter = new StreamWriter(isoFileStream))

        {

            isoFileWriter.WriteLine(txtWrite.Text);

        }

    }



//将文字读出来

    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();



    try

    {

        // Specify the file path and options.

        using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.Open, myStore))

        {

            // Read the data.

            using (var isoFileReader = new StreamReader(isoFileStream))

            {

                txtRead.Text = isoFileReader.ReadLine();

            }

        }



    }

    catch

    {

        // Handle the case when the user attempts to click the Read button first.

        txtRead.Text = "Need to create directory and the file first.";

    }

你可能感兴趣的:(wp7)