Exemplo n.º 1
0
        public void LoadDatabase(string path = null)
        {
            if (String.IsNullOrWhiteSpace(path))
            {
                path = Program.Options.DataFile;
            }

            if (!File.Exists(Program.Options.DataFile))
            {
                using (var writer = File.CreateText(path)) { // Create the file
                    writer.Write("{\n}");                    // Write to the stream and flush it
                    writer.Flush();
                }
            }

            using (var reader = new StreamReader(path, Encoding.UTF8)) {
                JsonReader jsonReader = new JsonTextReader(reader);
                // Try to deserialize the stream
                JsonSerializer serializer = new JsonSerializer();
                ImageDataList  imgList    = serializer.Deserialize <ImageDataList> (jsonReader);
                ImageDataList.Images.Clear();
                if (imgList != null && imgList.imageList != null)
                {
                    ImageDataList.Images.AddRange(imgList.imageList);
                }

                imgList = null;     // Null the temporary image list
                jsonReader.Close(); // Close the jsonReader
            }

            SendOnChange(ImageDataChangeType.Loading);
        }
Exemplo n.º 2
0
        public void SaveDatabase(string path = null)
        {
            if (String.IsNullOrWhiteSpace(path))
            {
                path = Program.Options.DataFile;
            }

            ImageDataList imagesList = new ImageDataList();

            imagesList.imageList = ImageDataList.Images.ToArray();
            JsonSerializer serializer = new JsonSerializer();

            using (var writer = new StreamWriter(path)) {
                serializer.Serialize(writer, imagesList, typeof(ImageDataList));
                writer.Flush();
            }
        }