/// <summary> /// Stores the cache in a binary file. /// </summary> /// <param name="cache">The library cache to be stored.</param> public static void StoreCacheFile(ComponentsLibraryCache cache) { cache.WasModified = false; try { #if DEBUG Console.WriteLine("Storing components library cache file to " + s_defaultCacheLocation); #endif using (FileStream fs = File.Create(s_defaultCacheLocation)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, cache); } } catch (Exception) { cache.Clear(); } }
/// <summary> /// Tries loading the cache from a binary file. If reading fails, an empty cache is returned instead. /// </summary> /// <returns> /// The components library cache. /// </returns> public static ComponentsLibraryCache LoadCacheFile() { ComponentsLibraryCache cache = null; try { using (FileStream fs = File.OpenRead(s_defaultCacheLocation)) { BinaryFormatter formatter = new BinaryFormatter(); cache = (ComponentsLibraryCache)formatter.Deserialize(fs); } } catch (Exception) // Cache file probably corrupted { DeleteCacheFile(); cache = null; } return(cache); }