From 1c03d85261d09499dc4cadad5bccfe9aecaedfc6 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Tue, 5 Aug 2014 00:12:50 +0200 Subject: [PATCH] WeatherInformation WP8: using temporary file Hopefully NTFS rename atomic. --- .../WeatherInformation/App.xaml.cs | 30 ++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs index 338023f..50952be 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs @@ -239,13 +239,33 @@ namespace WeatherInformation }; } - // Temporary file? NO RIGHT WAY WITH WINDOWS :O Windows sucks? AFAIK YES. - // rename is not atomic (AFAIK) and destination file must not exist... ROFL - // Microsoft should learn from UNIX: http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/ - private void SaveDataToIsolatedStorage(string isoFileName, string value) + private void SaveDataToIsolatedStorage(string fileName, string value) + { + string pathToTemporaryFile = CreateTemporaryFile(fileName); + + SaveDataToTemporaryFile(pathToTemporaryFile, value); + + using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) + { + // Hopefully NTFS rename atomic... No too much information :/ + isoStore.MoveFile(pathToTemporaryFile, fileName); + } + } + + private string CreateTemporaryFile(string fileName) + { + using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) + { + isoStore.CreateDirectory("tmp"); + } + + return String.Concat("tmp/", fileName); + } + + private void SaveDataToTemporaryFile(string pathToTemporaryFile, string value) { using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) - using (IsolatedStorageFileStream fileStream = isoStore.OpenFile(isoFileName, FileMode.OpenOrCreate)) + using (IsolatedStorageFileStream fileStream = isoStore.OpenFile(pathToTemporaryFile, FileMode.Create)) using (StreamWriter sw = new StreamWriter(fileStream)) { sw.Write(value); -- 2.1.4