3
Answers

Getting text files data from folder

JOHN JOHNNNY

JOHN JOHNNNY

9y
525
1
Hi

The code below works fine for reading json files from my project folder, the challenge am having is how to rewrite the code or changes i need to make to read text files from my project folder i have tried but to no avail.
kindly help

//code to read json files
public partial class BibleInYear : PhoneApplicationPage
    {

        private List<BibleData> devotions;

        public BibleInYear()
        {
            InitializeComponent();

            devotions = new List<BibleData>();
            AddDevotions();   
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            DateTime dt = DateTime.Now;
            int month = dt.Month;
            int year = dt.Year;
            int index;

            if (DateTime.IsLeapYear(year) || (month <= 2))
            {
                index = dt.DayOfYear - 1; // list is indexed from 0
            }
            else
            {
                index = dt.DayOfYear; // add a day
            }

            textblock.Text = devotions[index].ToString(); // or some other property
        }

        private void AddDevotions()
        {
            for (int i = 1; i <= 366; i++)
            {
                string filePath = "BibleInYear/Bible" + i.ToString() + ".json";
                BibleData d = ReadJsonFile(filePath);
                devotions.Add(d);
            }
        }

        public BibleData ReadJsonFile(string JsonfilePath)
        {
            BibleData[] d = null;

            using (StreamReader r = new StreamReader(JsonfilePath))
            {
                string json = r.ReadToEnd();
                d = JsonConvert.DeserializeObject<BibleData[]>(json);
            }
            return d[0];
        }

Thank you in advance and reply soon
Answers (3)