CreateDirectory Method

IsolatedStorageFile.CreateDirectory Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Creates a directory in the isolated storage scope.

Namespace:  System.IO.IsolatedStorage
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Sub CreateDirectory ( _
	dir As String _
)

Parameters

dir
Type: System.String
The relative path of the directory to create within the isolated storage.

ExceptionCondition
IsolatedStorageException

The store has been removed.

-or-

Unable to create directory.

-or-

Isolated storage is disabled.

ArgumentException

dir is badly formed.

ArgumentNullException

The directory path is Nothing.

ObjectDisposedException

The store has been disposed.

The created directory initially contains no files. If the directory already exists, the CreateDirectory method returns without creating a new directory.

The following example creates directories and subdirectories. This example is part of a larger example provided for IsolatedStorageFile class.


Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Input
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Collections
Imports System.Text


Class Example

    Public Shared Sub Demo(ByVal outputBlock As TextBlock)
        ' Obtain an isolated store for an application.
        Try
            Using store As IsolatedStorageFile = _
            	IsolatedStorageFile.GetUserStoreForApplication()
                ' Use a StringBuilder to construct output.
                Dim sb As StringBuilder = New StringBuilder

                ' Create three directories in the root.
                store.CreateDirectory("MyApp1")
                store.CreateDirectory("MyApp2")
                store.CreateDirectory("MyApp3")

                ' Create three subdirectories under MyApp1.
                Dim subdirectory1 As String = Path.Combine("MyApp1", "SubDir1")
                Dim subdirectory2 As String = Path.Combine("MyApp1", "SubDir2")
                Dim subdirectory3 As String = Path.Combine("MyApp1", "SubDir3")
                store.CreateDirectory(subdirectory1)
                store.CreateDirectory(subdirectory2)
                store.CreateDirectory(subdirectory3)

                ' Create a file in the root.
                Dim rootFile As IsolatedStorageFileStream = _
                    store.CreateFile("InTheRoot.txt")
                rootFile.Close()


                ' Create a file in a subdirectory.
                Dim subDirFile As IsolatedStorageFileStream = _
                    store.CreateFile(Path.Combine(subdirectory1, "MyApp1A.txt"))
                subDirFile.Close()

                ' Gather file information.
                Dim directoriesInTheRoot() As String = store.GetDirectoryNames

                Dim filesInTheRoot() As String = store.GetFileNames

                Dim searchpath As String = Path.Combine(subdirectory1, "*.*")
                Dim filesInSubDirs() As String = store.GetFileNames(searchpath)

                ' Find subdirectories within the MyApp1
                ' directory using the multi character '*' wildcard.
                Dim subDirectories() As String = _
                    store.GetDirectoryNames(Path.Combine("MyApp1", "*"))

                ' List file infomration

                ' List the directories in the root.
                sb.AppendLine("Directories in root:")
                For Each dir As String In directoriesInTheRoot
                    sb.AppendLine((" - " + dir))
                Next
                sb.AppendLine()
                ' List the subdirectories under MyApp1.
                sb.AppendLine("Directories under MyApp1:")
                For Each sDir As String In subDirectories
                    sb.AppendLine((" - " + sDir))
                Next
                sb.AppendLine()
                ' List files in the root.
                sb.AppendLine("Files in the root:")
                For Each fileName As String In filesInTheRoot
                    sb.AppendLine((" - " + fileName))
                Next
                sb.AppendLine()
                ' List files in MyApp1\SubDir1.
                sb.AppendLine("Files in MyApp1\SubDir1:")
                For Each fileName As String In filesInSubDirs
                    sb.AppendLine((" - " + fileName))
                Next
                sb.AppendLine()

                ' Write to an existing file: MyApp1\SubDir1\MyApp1A.txt

                ' Determine if the file exists before writing to it.
                Dim filePath As String = Path.Combine(subdirectory1, "MyApp1A.txt")

                If store.FileExists(filePath) Then
                    Try
                        Using sw As StreamWriter = _
                            New StreamWriter(store.OpenFile(filePath, FileMode.Open, FileAccess.Write))

                            sw.WriteLine("To do list:")
                            sw.WriteLine("1. Buy supplies.")
                        End Using

                    Catch ex As IsolatedStorageException
                        sb.AppendLine(ex.Message)
                    End Try
                Else
                    sb.AppendLine((filePath + "does not exist"))
                End If

                ' Read the contents of the file: MyApp1\SubDir1\MyApp1A.txt
                Try
                    Using reader As StreamReader = _
                        New StreamReader(store.OpenFile(filePath, FileMode.Open, FileAccess.Read))
                        Dim contents As String = reader.ReadToEnd
                        sb.AppendLine(filePath + " contents:")
                        sb.AppendLine(contents)
                    End Using
                Catch ex As IsolatedStorageException
                    sb.Append(ex.Message)
                End Try


                ' Delete a file.
                Try
                    If store.FileExists(filePath) Then
                        store.DeleteFile(filePath)
                    End If
                Catch ex As IsolatedStorageException
                    sb.AppendLine(ex.Message)
                End Try

                ' Delete a specific directory.
                Dim dirDelete As String = Path.Combine("MyApp1", "SubDir3")
                Try
                    If store.DirectoryExists(dirDelete) Then
                        store.DeleteDirectory(dirDelete)
                    End If
                Catch ex As IsolatedStorageException
                    sb.AppendLine(ex.Message)
                End Try

                sb.AppendLine()
                ' remove the store
                store.Remove()

                sb.AppendLine("Store removed.")
                outputBlock.Text = sb.ToString()
            End Using
        Catch Ex As IsolatedStorageException
            ' TODO: Handle that store was unable to be accessed.
        End Try
    End Sub
End Class

' Quota status in increase examples
Class StoreQuota

    ' Assumes an event handler for the MouseLeftbuttonUp
    ' event is defined for a control named 'IncreaseQuota'
    ' In the control's XAML: MouseLeftButtonUp="IncreaseQuota_OnLeftMouseButtonUp"
    Public Sub IncreaseQuota_OnClick(ByVal sender As Object, ByVal e As MouseEventArgs)
        ' Obtain an isolated store for an application.
        Try
            Using store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
                ' Request 5MB more space in bytes.
                Dim spaceToAdd As Int64 = 5242880
                Dim curAvail As Int64 = store.AvailableFreeSpace
                ' If available space is less than
                ' what is requested, try to increase.
                If (curAvail < spaceToAdd) Then
                    ' Request more quota space.
                    If Not store.IncreaseQuotaTo((store.Quota + spaceToAdd)) Then
                        ' The user clicked NO to the
                        ' host's prompt to approve the quota increase.
                    Else
                        ' The user clicked YES to the
                        ' host's prompt to approve the quota increase.
                    End If
                End If
            End Using
        Catch Ex As IsolatedStorageException

            ' TODO: Handle that store could not be accessed.
        End Try
    End Sub




    Public Shared Sub ShowIsoStoreStatus(ByVal inputBlock As TextBlock)
        ' Obtain an isolated store for an application.
        Try
            Using store As IsolatedStorageFile = _
              IsolatedStorageFile.GetUserStoreForApplication()
                Dim spaceUsed As String = (store.Quota - store.AvailableFreeSpace).ToString
                Dim spaceAvailable As String = store.AvailableFreeSpace.ToString
                Dim curQuota As String = store.Quota.ToString
                inputBlock.Text = String.Format("Quota: {0} bytes, Used: {1} bytes, Available: {2} bytes", _
                                         curQuota, spaceUsed, spaceAvailable)
            End Using
        Catch Ex As IsolatedStorageException
            inputBlock.Text = "Unable to access store."
        End Try
    End Sub
End Class

' This example's Example.Demo method 
' produces the following output:
' -----
' Directories in root:
' - MyApp1
' - MyApp2
' - MyApp3

' Directories under MyApp1:
' - SubDir1
' - SubDir2
' - SubDir3

' Files in the root:
' - InTheRoot.txt

' Files in MyApp1\SubDir1:
' - MyApp1A.txt

' MyApp1\SubDir1\MyApp1A.txt contents:
' To do list:
' 1. Buy supplies.

' Store removed.
' -----



Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2016 Microsoft