Пример #1
0
        private void ReadWaterFile()
        {
            try
            {
                // Read the list from water file
                string path = Environment.GetFolderPath(
                    Environment.SpecialFolder.LocalApplicationData);
                string filename = Path.Combine(path, "RecordWater_Day.txt");

                // use a stream reader to read the text out to file
                using (var streamReader = new StreamReader(filename, false))
                {
                    string jsonText = streamReader.ReadToEnd();
                    record = JsonConvert.DeserializeObject <List <RecordWaterDay> >(jsonText);
                }
                RecordWaterDay objs = new RecordWaterDay();
                foreach (var obj in record)
                {
                    this.day        = obj.day;
                    this.month      = obj.month;
                    this.totalDrank = obj.totalDrank;
                }
            }
            catch (FileNotFoundException)
            {
                // need a link to the assembly (dll) to get the file
                var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
                // create a stream to access the file
                Stream stream = assembly.GetManifestResourceStream(
                    "waterAppDev2018.Model.WaterStore.txt");
                using (var reader = new StreamReader(stream))
                {
                    string jsonText = reader.ReadToEnd();
                    record = JsonConvert.DeserializeObject <List <RecordWaterDay> >(jsonText);
                }

                RecordWaterDay wd = new RecordWaterDay();
                foreach (var obj in record)
                {
                    this.day        = obj.day; // all 0
                    this.month      = obj.month;
                    this.year       = obj.year;
                    this.totalDrank = obj.totalDrank;
                }
            }
        }
Пример #2
0
        private void WriteWaterFile()
        {
            RecordWaterDay recordWater = new RecordWaterDay();

            recordWater.day        = this.day;
            recordWater.month      = this.month;
            recordWater.totalDrank = this.totalDrank;
            record.Add(recordWater);  // filling and adding entry to list

            // write the list to a local file
            string path = Environment.GetFolderPath(
                Environment.SpecialFolder.LocalApplicationData);
            string filename = Path.Combine(path, "RecordWater_Day.txt");

            // use a stream writer to write the text out to file
            using (var streamWriter = new StreamWriter(filename, false))
            {
                // serialise the dogs list to a string using the jsonconvert library
                string jsonText = JsonConvert.SerializeObject(record);
                streamWriter.WriteLine(jsonText);
            }
        }
 public RecordWaterDay()
 {
     RecordWaterDay d = new RecordWaterDay(this.day, this.month, this.year, this.totalDrank);
 }