WeatherInformation WP8: using temporary file
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Mon, 4 Aug 2014 22:12:50 +0000 (00:12 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Mon, 4 Aug 2014 22:12:50 +0000 (00:12 +0200)
Hopefully NTFS rename atomic.

WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs

index 338023f..50952be 100644 (file)
@@ -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);