将日期保存到文本文件中

时间:2011-03-09 22:20:00

标签: c# visual-studio windows-phone-7

我正在开发一个小型Windows Phone应用程序。它需要能够将当前日期保存为文本文件的名称。现在,我有以下代码:

{
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            isf.CreateDirectory("Data");
            StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("Data\\myFile.txt", FileMode.Create, isf));
            sw.WriteLine(textBox1.Text);
            sw.Close();
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(new IsolatedStorageFileStream("Data\\myFile.txt", FileMode.Open, isf));
                textBlock1.Text = sr.ReadLine();
                sr.Close();
            }

            catch
            {
                textBox1.Text = "When you make a Journal entry, it will be displayed here.";
            }
        }
        private void textBlock1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            isf.CreateDirectory("Data");
            StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("Data\\myFile.txt", FileMode.Create, isf));
            sw.WriteLine(textBox1.Text);
            sw.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {

        }

        private void textBlock1_Loaded(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(new IsolatedStorageFileStream("Data\\myFile.txt", FileMode.Open, isf));
                textBlock1.Text = sr.ReadLine();
                sr.Close();
            }

            catch
            {
                textBlock1.Text = "When you make a Journal entry, it will be displayed here.";
            }
        }

抱歉,“嵌入代码”功能不喜欢我的代码。

编辑:感谢无论是谁修改了“嵌入代码”功能。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

不使用硬编码myFile.txt,只需使用DateTime.Today建立文件名即可。例如:

string fileName = DateTime.Today.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);

然后您可以创建StreamWriter

StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("Data\\" + fileName, FileMode.Create, isf));

答案 1 :(得分:0)

我想我理解你的问题。您希望每天创建一个新的文本文件,并将文件命名为“03-09-2011.txt”。

你可以这样做:

var fileName = string.Format("{0:MM-dd-yyyy}.txt", DateTime.Now);

这将得到一个像我上面提到的文件名。

我也可能会使用Path.Combine:

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
var directory = "Data";
isf.CreateDirectory(directory);
var fileName = string.Format("{0:MM-dd-yyyy}.txt", DateTime.Now);
StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream(Path.Combine(directory, filName), FileMode.Create, isf));
sw.WriteLine(textBox1.Text);
sw.Close();

这将在路径之间添加“\”,这样你就可以将目录名和文件名分开(我以后似乎总是需要这些)。

这有一个额外的好处,你可以轻松地添加或附加到名称:

var fileName = string.Format("This is the file for {0:MM-dd-yyyy}.txt", DateTime.Now);