Example #1
0
        /// <summary>
        /// Deserializes object from isolated storage file.
        /// </summary>
        /// <typeparam name="T">Type of object.</typeparam>
        /// <param name="fileName">Path to isolated storage file.</param>
        /// <returns></returns>
        public static T Deserialize <T>(string subDirectory, string fileName)
        {
            try
            {
                // Open isolated storage.
                using (var storageManager = new IsolatedStorageManager())
                {
                    fileName = Prepare(storageManager, subDirectory, fileName);

                    if (storageManager.FileExists(fileName))
                    {
                        // Open file from storage.
                        using (var stream = storageManager.OpenFile(fileName, IO.OpenFileMode.Open))
                        {
                            XFile file = XFile.LoadBinary(stream, fileName);

                            var serializer = new XSerializer(file);

                            return(serializer.Deserialize <T>("Data"));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }

            return(default(T));
        }
Example #2
0
        /// <summary>
        /// Serializes object to isolated storage file.
        /// </summary>
        /// <param name="fileName">file name.</param>
        /// <param name="obj">Object to serialize.</param>
        public static void Serialize(string subDirectory, string fileName, Object obj)
        {
            // Open isolated storage.
            using (var storageManager = new IsolatedStorageManager())
            {
                fileName = Prepare(storageManager, subDirectory, fileName);

                // Open file from storage.
                using (Stream stream = storageManager.OpenFile(fileName, IO.OpenFileMode.Create))
                {
                    XFile file = XFile.Create(fileName);

                    XNode fileNode = file;

                    var node = new XNode(file, "Serializator");
                    ((XNode)file).Nodes.Add(node);

                    // Create serializer for type.
                    var serializer = new XSerializer(node);
                    serializer.Serialize("Data", obj);

                    file.WriteBinary(stream);
                }
            }
        }
Example #3
0
        public static string LoadLog(bool removeExisting)
        {
            using (var storageManager = new IsolatedStorageManager())
            {
                string text;
                using (var stream = storageManager.OpenFile("ServiceMode.log", IO.OpenFileMode.Open))
                {
                    var reader = new StreamReader(stream);
                    text = reader.ReadToEnd();
                }

                if (removeExisting)
                {
                    storageManager.DeleteFile("ServiceMode.log");
                }

                return(text);
            }
        }
Example #4
0
        internal static void Update()
        {
            if (_flush)
            {
                string data = null;
                lock (_serviceModeLock)
                {
                    data = _cache.ToString();
                    _cache.Clear();
                    _flush = false;
                }

                using (var storageManager = new IsolatedStorageManager())
                {
                    using (var stream = storageManager.OpenFile("ServiceMode.log", IO.OpenFileMode.Append))
                    {
                        var writer = new StreamWriter(stream);
                        writer.AutoFlush = true;
                        writer.Write(data);
                    }
                }
            }
        }