简体   繁体   English

写入IsolatedStorageFile的问题

[英]Problems with writing into IsolatedStorageFile

I'm trying to write data into a file using IsolatedStorageFile. 我正在尝试使用IsolatedStorageFile将数据写入文件。 The file is called "Notes". 该文件称为“注释”。 The notes file is not opened at all! 笔记文件根本没有打开!

While debugging, I found that the control goes to the else condition. 在调试时,我发现控件进入了else条件。 It creates the "Notes.txt" file, but it does not further enter into the loop. 它会创建“ Notes.txt”文件,但不会进一步进入循环。 It executes the using statement and then comes out of the loop and hence the writing does not take place. 它执行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 { }
    }    

Could somebody help me on this? 有人可以帮我吗?

You create Notes.txt but check for Notes. 您创建Notes.txt,但检查Notes。 Check for Notes.txt instead. 而是检查Notes.txt。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM