alfah
alfah

Reputation: 2085

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?

Upvotes: 0

Views: 142

Answers (2)

alfah
alfah

Reputation: 2085

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

had to be used in the else block and not FileMode.Open

Upvotes: 0

Filip Skakun
Filip Skakun

Reputation: 31724

You create Notes.txt but check for Notes. Check for Notes.txt instead.

Upvotes: 3

Related Questions