From: gu.martinm@gmail.com Date: Mon, 4 Aug 2014 22:12:50 +0000 (+0200) Subject: WeatherInformation WP8: using temporary file X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=1c03d85261d09499dc4cadad5bccfe9aecaedfc6;p=CSharpForFun%2F.git WeatherInformation WP8: using temporary file Hopefully NTFS rename atomic. --- 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);