Skip to content

Instantly share code, notes, and snippets.

@GSonofNun
Created February 16, 2014 20:57
Show Gist options
  • Save GSonofNun/9040551 to your computer and use it in GitHub Desktop.
Save GSonofNun/9040551 to your computer and use it in GitHub Desktop.
public async Task ObjectToFile(Object obj, string fileName, string path = null)
{
string json = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(obj));
StorageFolder sf = null;
if (path != null)
{
sf = await ApplicationData.Current.LocalFolder.GetFolderAsync(path);
}
else
{
sf = ApplicationData.Current.LocalFolder;
}
StorageFile file = await sf.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (StreamWriter sw = new StreamWriter(await file.OpenStreamForWriteAsync()))
{
await sw.WriteAsync(json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment