4

I'm writing a Windows Phone Silverlight app. I want to save an object to a JSON file. I've written the following piece of code.

string jsonFile = JsonConvert.SerializeObject(usr);
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("users.json", FileMode.Create, isoStore);

StreamWriter str = new StreamWriter(isoStream);
str.Write(jsonFile);

This is enough to create a JSON file but it is empty. Am I doing something wrong? Wasn't this supposed to write the object to the file?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
nick
  • 1,611
  • 4
  • 20
  • 35
  • 3
    Did you [close](https://msdn.microsoft.com/en-us/library/system.io.streamwriter.close(v=vs.110).aspx) the `StreamWriter`? Or better yet wrap it in a `using (var str = new StreamWriter(isoStream)) { str.Write(jsonFile); }` statement? See [how to use StreamWriter class properly?](https://stackoverflow.com/questions/11467240/how-to-use-streamwriter-class-properly). – dbc Apr 22 '16 at 06:26
  • No I didn't close it. – nick Apr 22 '16 at 06:28
  • Well that's the problem then. If you don't close the `StreamWriter` some of the contents you have written may not get flushed to disk. – dbc Apr 22 '16 at 06:30

2 Answers2

4

The problem is that you're not closing the stream.

File I/O in Windows have buffers at the operating system level, and .NET might even implement buffers at the API level, which means that unless you tell the class "Now I'm done", it will never know when to ensure those buffers are propagated all the way down to the platter.

You should rewrite your code just slightly, like this:

using (StreamWriter str = new StreamWriter(isoStream))
{
    str.Write(jsonFile);
}

using (...) { ... } will ensure that when the code leaves the block, the { ... } part, it will call IDisposable.Dispose on the object, which in this case will flush the buffers and close the underlying file.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
0

I use these. Shoud work for you as well.

    public async Task SaveFile(string fileName, string data)
    {
        System.IO.IsolatedStorage.IsolatedStorageFile local =
            System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

        if (!local.DirectoryExists("MyDirectory"))
            local.CreateDirectory("MyDirectory");

        using (var isoFileStream =
                new System.IO.IsolatedStorage.IsolatedStorageFileStream(
                    string.Format("MyDirectory\\{0}.txt", fileName),
                    System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite,
                        local))
        {
            using (var isoFileWriter = new System.IO.StreamWriter(isoFileStream))
            {
                await isoFileWriter.WriteAsync(data);
            }
        }
    }

    public async Task<string> LoadFile(string fileName)
    {
        string data;

        System.IO.IsolatedStorage.IsolatedStorageFile local =
            System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

        using (var isoFileStream =
                new System.IO.IsolatedStorage.IsolatedStorageFileStream
                    (string.Format("MyDirectory\\{0}.txt", fileName),
                    System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read,
                    local))
        {
            using (var isoFileReader = new System.IO.StreamReader(isoFileStream))
            {
                data = await isoFileReader.ReadToEndAsync();
            }
        }

        return data;
    }