文本文件未按日期显示 [英] Text files not displaying according to date

查看:70
本文介绍了文本文件未按日期显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在开发一个在我的本地文件夹中有365个文本文件的应用程序,应用程序的本质是根据一年中的某一天显示文本文件的每个内容,例如2015年2月3日它将显示Devotion34.txt但是当我在模拟器上运行它时显示5月14日是Devotion134.txt,即它在一年之前跳过100天

这个我的代码在下面请帮助

xaml.cs



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

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

推荐答案

您的代码实际上是在添加在读取的数字前面1:



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

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





从/ Devotion中删除1,它应该可以工作。



Remove the 1 from /Devotion and it should work.


这篇关于文本文件未按日期显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆