简体   繁体   中英

Problems with writing into IsolatedStorageFile

I'm trying to write data into a file using 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. It creates the "Notes.txt" file, but it does not further enter into the loop. It executes the using statement and then comes out of the loop and hence the writing does not take place.

    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. Check for Notes.txt instead.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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