Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am developing an app that has 365 text files in my local folder, the essence of the app is to display each content of the text file according to the day of the year e.g for feb 3 2015 it will display Devotion34.txt but when i run it on the emulator is displaying May 14 which is Devotion134.txt i.e it jumps 100 days ahead of the year
This my code below kindly help
xaml.cs

C#
public partial class MainPage : PhoneApplicationPage
    {
        List<string> devotions = new List<string>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            AddDevotions();
            int index = DateTime.Now.DayOfYear;
            textblock.Text = devotions[index];

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        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 = Path.GetFullPath("Devotions/Devotion1" + i.ToString() + ".txt");

                if (File.Exists(filePath))
                {
                    devotions.Add(ReadTextFile(filePath));
                }
                else
                {
                    devotions.Add(string.Format("Devotions file '{0}' was not found.", i));
                }
            }
        }

        public string ReadTextFile(string textFilePath)
        {
            string text = null;

            using (StreamReader r = new StreamReader(textFilePath))
            {
                text = r.ReadToEnd();
            }

            return text;
        }

Reply soon and thank you in advance
Posted
Updated 3-Feb-15 3:08am
v2

1 solution

Your code is actually adding the 1 in front of the number being read:

C#
string filePath = Path.GetFullPath("Devotions/Devotion1" + i.ToString() + ".txt");


Remove the 1 from /Devotion and it should work.
 
Share this answer
 
Comments
Member 10627743 3-Feb-15 9:15am    
Thank you RyanDev
ZurdoDev 3-Feb-15 9:18am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900