WP7 File Helpers


Here’s with some static methods that will help with reading/write to isolated storage (and deleting/verifying that a file exists). The ReadAllText and WriteAllText methods behave similar to what you would see in the VB.Net “My” namespace in the full framework.

    Imports System.IO
    Namespace IO
    
        ''' <summary>
        ''' File utilities for use in isolated storage environments.
        ''' </summary>
        ''' <remarks>
        ''' This code was written to support isolated storage in Silverlight for WP7.
        ''' </remarks>
        Public Class File
            '*********************************************************************************************************************
            '
            '             Class:  File
            '      Initial Date:  03/24/2011
            '      Last Updated:  11/04/2011
            '     Programmer(s):  Blake Pell
            '
            '*********************************************************************************************************************
    
            ''' <summary>
            ''' Writes all text to the specified file in isolated storage.
            ''' </summary>
            ''' <param name="filename">The name of the file to write to.</param>
            ''' <param name="textToWrite">The text to write to the file.</param>
            ''' <param name="append">Whether or not the file should be appended to.  If the value is false, the file
            ''' will be truncated before it is written to.</param>
            ''' <remarks>
            ''' If the file does not exist, the file will be created via FileMode.CreateNew.
            ''' </remarks>
            Public Shared Sub WriteAllText(filename As String, textToWrite As String, append As Boolean)
                Dim fm As FileMode
                If append = True Then
                    fm = FileMode.Append
                Else
                    Using store As IsolatedStorage.IsolatedStorageFile = IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication
                        If store.FileExists(filename) = False Then
                            fm = FileMode.CreateNew
                        Else
                            fm = FileMode.Truncate
                        End If
                    End Using
                End If
                WriteAllText(filename, textToWrite, fm)
            End Sub
    
            ''' <summary>
            ''' Writes all text to the specified file in isolated storage.
            ''' </summary>
            ''' <param name="filename">The name of the file to write to.</param>
            ''' <param name="textToWrite">The text to write to the file.</param>
            ''' <param name="fm">The FileMode to use.  This will allow you control over how to handle the file</param>
            ''' <remarks>No checks are performed with this overload to whether the file exists or not.<br /><br />
            ''' This code was written to support isolated storage in Silverlight for WP7.
            ''' </remarks>
            Public Shared Sub WriteAllText(filename As String, textToWrite As String, fm As FileMode)
                Using store As IsolatedStorage.IsolatedStorageFile = IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication
                    Using ifs As New IsolatedStorage.IsolatedStorageFileStream(filename, fm, store)
                        Using st As New StreamWriter(ifs)
                            st.Write(textToWrite)
                            st.Close()
                        End Using
                        ifs.Close()
                    End Using
                End Using
            End Sub
    
            ''' <summary>
            ''' Reads all text from the specified file in isolated storage.
            ''' </summary>
            ''' <param name="filename"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' This code was written to support isolated storage in Silverlight for WP7.
            ''' </remarks>
            Public Shared Function ReadAllText(filename As String) As String
                Dim buf As String = ""
                Using store As IsolatedStorage.IsolatedStorageFile = IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication
                    If store.FileExists(filename) = False Then
                        Return ""
                    End If
                    Using ifs As New IsolatedStorage.IsolatedStorageFileStream(filename, FileMode.Open, store)
                        Using st As New StreamReader(ifs)
                            buf = st.ReadToEnd()
                            st.Close()
                        End Using
                        ifs.Close()
                    End Using
                End Using
                Return buf
            End Function
    
            ''' <summary>
            ''' Initializes the isolated storage and checks whether a file exists.
            ''' </summary>
            ''' <param name="filename"></param>
            Public Shared Function FileExists(filename As String) As Boolean
                Using store As IsolatedStorage.IsolatedStorageFile = IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication
                    Return store.FileExists(filename)
                End Using
            End Function
    
            ''' <summary>
            ''' Deletes a file from IsolatedStorage.
            ''' </summary>
            ''' <param name="filename"></param>
            Public Shared Sub DeleteFile(filename As String)
                Using store As IsolatedStorage.IsolatedStorageFile = IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication
                    store.DeleteFile(filename)
                End Using
            End Sub
    
        End Class
    End Namespace

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.