Raghav Manikandan
Raghav Manikandan

Reputation: 679

Windows storage in windows 8 app

I use the following codes to save some contents into the IsolatedStorage for windows phone apps-

    public void SaveContent(string content)
    {
        using (IsolatedStorageFile userStore =
        IsolatedStorageFile.GetUserStoreForApplication())
        using (IsolatedStorageFileStream stream =
        userStore.CreateFile(this.Filename))
        using (StreamWriter writer = new StreamWriter(stream))
        {
            writer.Write(content);
        }
    }

Now i want to port the same to my windows 8 app but there are no similar codes in StorageFile for w8 i could find. Can anybody help me with this?

Upvotes: 0

Views: 296

Answers (2)

Christian Amado
Christian Amado

Reputation: 943

You can read more about Storage here.

Upvotes: 0

Marius Bancila
Marius Bancila

Reputation: 16318

You could use the LocalFolder.

var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

For writing you have several choices. If you have an IBuffer then you can use WriteBufferAsync

var file = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
var buffer = GetBufferFromString("this is an example");
await FileIO.WriteBufferAsync(file, buffer);

The other option is to write using a stream.

var file = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
   using(var outputStream = stream.GetOutputStreamAt(0))
   {
       var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream);
       dataWriter.WriteBytes(...);
       await dataWriter.StoreAsync();
       dataWriter.DetachStream();
       await outputStream.FlushAsync();
   }
}

You can read more here: Quickstart: Reading and writing files (Windows Store apps using C#/VB/C++ and XAML).

Upvotes: 2

Related Questions