C# Modifiers

Isolated Storage

To save or retrieve data as key/value pairs, you use the IsolatedStorageSettings class.

IsolatedStorageSettings is a dictionary that enables you to store strings and other objects. 

You typically use the following methods on IsolatedStorageSettings:




  • Add: Adds an entry to the dictionary for the key/value pair.
  • Contains: Determines whether the dictionary contains the specified key.
  • Remove: Removes the entry with the specified key.
To get or set the value that is associated with the specified key, you use the Item property. You typically use the following syntax. If the key does not already exist, a new entry is added to the dictionary.
myCollection["myKey"] = myValue (C#)
myCollection("myKey") = myValue (Visual Basic)
The following code demonstrates how to add and remove a key/value pair.

C#

// Create an instance of IsolatedStorageSettings.
private IsolatedStorageSettings userSettings =
    IsolatedStorageSettings.ApplicationSettings;
// Add a key and its value.
userSettings.Add("userImage", "BlueHills.jpg");
// Remove the setting.
userSettings.Remove("userImage");

Visual Basic

' Create an instance of IsolatedStorageSettings.
Private userSettings As IsolatedStorageSettings = _
    IsolatedStorageSettings.ApplicationSettings
' Add a key and its value.
userSettings.Add("userImage", "BlueHills.jpg")
' Remove the setting.
userSettings.Remove("userImage")

Working with Files

To save or retrieve files, you use the IsolatedStorageFile class. IsolatedStorageFile represents an isolated storage area that contains files and directories. When you work with files, you also typically use the IsolatedStorageFileStream class to read and write content to the file. You typically use the following methods on IsolatedStorageFile:
  • GetUserStoreForApplication: Obtains user-scoped isolated storage that corresponds to the calling code's application identity.
  • FileExists: Determines whether the specified path refers to an existing file in the isolated store.
  • CreateFile: Creates a file in the isolated store.
  • OpenFile: Opens a file at a specified path using the specified sharing and access options. This method returns an IsolatedStorageFileStream object that contains the file's stream.
  • DeleteFile: Deletes a file in the isolated store.
  • DirectoryExists: Determines whether the specified path refers to an existing directory in the isolated store.
  • CreateDirectory: Creates a directory in the isolated storage scope.
  • DeleteDirectory: Deletes a directory in the isolated storage scope.
  • Remove: Removes the isolated storage scope and all its contents.
Because isolated storage uses unmanaged resources, an instance of the IsolatedStorageFile class and the IsolatedStorageFileStream class should be disposed after their use. The using statement does this for you automatically and is a good practice to use. The following code demonstrates some of theIsolatedStorageFile and IsolatedStorageFileStream methods.
To download all the code for this example, see Isolated Storage example download.

C#

// Obtain the isolated storage for an application.
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    // Create a directory at the root of the store.
    if (!store.DirectoryExists("Images"))
    {
        store.CreateDirectory("Images");
    }

    // Get the stream of an existing file,
    // or create a file and get its stream.

    using (var isoStream = 
        store.OpenFile(@"Images\UserImageFile.jpg", FileMode.OpenOrCreate))
    {

        ...

        // Write to the stream.
        isoStream.Write(bytes, 0, numBytesToRead);
    }

    ...

    // Remove isolated storage.
    store.Remove();
}

Visual Basic

' Obtain the isolated storage for an application.
Using store As IsolatedStorageFile = _
    IsolatedStorageFile.GetUserStoreForApplication()

    ' Create a directory at the root of the store.
    If Not store.DirectoryExists("Images") = True Then
        store.CreateDirectory("Images")
    End If

    ' Get the stream of an existing file,
    ' or create a file and get its stream.

    Using isoStream As IsolatedStorageFileStream = _
        store.OpenFile("Images\UserImageFile.jpg", FileMode.OpenOrCreate)

        ...

        ' Write to the stream.
        isoStream.Write(bytes, 0, numbytesToRead)

    End Using

    ...

    ' Remove isolated storage data.
    store.Remove()
End Using

fonte> http://www.silverlight.net/learn/graphics/file-and-local-data/isolated-storage-(silverlight-quickstart)

Comments