C# 将日期保存到文本文件中

C# 将日期保存到文本文件中,c#,visual-studio,windows-phone-7,C#,Visual Studio,Windows Phone 7,我正在开发一个小的Windows Phone应用程序。它需要能够将当前日期保存为文本文件的名称。现在,我有以下代码: { IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); isf.CreateDirectory("Data"); StreamWriter sw = new StreamWriter(new IsolatedS

我正在开发一个小的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.";
            }
        }
抱歉,“嵌入代码”功能不喜欢我的代码。 编辑:感谢修复了“嵌入代码”功能的任何人。
任何帮助都将不胜感激。

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

string fileName = DateTime.Today.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
然后,您可以创建
StreamWriter

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

我想我理解你的问题了。您希望每天创建一个新的文本文件,并将该文件命名为“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);
谢谢最后一位(StreamWriter sw=新StreamWriter…)就是我要找的。