写入IsolatedStorageFile的问题

时间:2011-11-18 06:58:24

标签: c# windows-phone-7 isolatedstorage

我正在尝试使用IsolatedStorageFile将数据写入文件。该文件名为“Notes”。注释文件根本没有打开!

在调试时,我发现控件进入了else条件。它会创建“Notes.txt”文件,但不会进一步进入循环。它执行using语句然后退出循环,因此写入不会发生。

    public void WriteNotesToFile()
    {
        try
        {
            using (IsolatedStorageFile storagefile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storagefile.FileExists("Notes"))
                {
                    using (IsolatedStorageFileStream fileStream = storagefile.OpenFile("Notes", FileMode.Open, FileAccess.ReadWrite))
                    {
                        StreamWriter writer = new StreamWriter(fileStream);

                        for (int i = 0; i < m_dtNoteDate.Length; i++)
                        {
                            writer.Write(m_dtNoteDate[i].ToString("dd-MM-yyy");
                            writer.Write(" ");
                            writer.WriteLine(m_strNotes[i]);
                        }
                        writer.Close();
                    }
                }
                else
                {
                    storagefile.CreateFile("Notes.txt");
                    using (IsolatedStorageFileStream fileStream = storagefile.OpenFile("Notes", FileMode.Open, FileAccess.ReadWrite))
                    {
                        StreamWriter writer = new StreamWriter(fileStream);

                        for (int i = 0; i < m_dtNoteDate.Length; i++)
                        {
                            writer.Write(m_dtNoteDate[i]);
                            writer.Write("");
                            writer.WriteLine(m_strNotes[i]);
                        }
                        writer.Close();
                    }
                }
            }
        }catch { }
    }    

有人可以帮我吗?

2 个答案:

答案 0 :(得分:3)

您创建Notes.txt但检查Notes。请检查Notes.txt。

答案 1 :(得分:0)

using (IsolatedStorageFileStream fileStream = storagefile.OpenFile("Notes", FileMode.Create, FileAccess.ReadWrite))

必须在else块中使用,而不是FileMode.Open