Documentation
-
How to: Read and Write to Files in Isolated Storage - .NET
Learn more about: How to: Read and Write to Files in Isolated Storage
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
After you've obtained an isolated store, you can create directories and files for storing data. Within a store, file and directory names are specified with respect to the root of the virtual file system.
To create a directory, use the IsolatedStorageFile.CreateDirectory instance method. If you specify a subdirectory of a directory that doesn't exist, both directories are created. If you specify a directory that already exists, the method returns without creating a directory, and no exception is thrown. However, if you specify a directory name that contains invalid characters, an IsolatedStorageException exception is thrown.
To create a file, use the IsolatedStorageFile.CreateFile method.
In the Windows operating system, isolated storage file and directory names are case-insensitive. That is, if you create a file named ThisFile.txt
, and then create another file named THISFILE.TXT
, only one file is created. The file name keeps its original casing for display purposes.
Isolated storage file creation will throw an IsolatedStorageException if the path contains a directory that does not exist.
The following code example illustrates how to create files and directories in an isolated store.
using System;
using System.IO;
using System.IO.IsolatedStorage;
public class CreatingFilesDirectories
{
public static void Main()
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null))
{
isoStore.CreateDirectory("TopLevelDirectory");
isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");
isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
Console.WriteLine("Created directories.");
isoStore.CreateFile("InTheRoot.txt");
Console.WriteLine("Created a new file in the root.");
isoStore.CreateFile("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt");
Console.WriteLine("Created a new file in the InsideDirectory.");
}
}
}
Imports System.IO
Imports System.IO.IsolatedStorage
Module Module1
Sub Main()
Using isoStore As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)
isoStore.CreateDirectory("TopLevelDirectory")
isoStore.CreateDirectory("TopLevelDirectory/SecondLevel")
isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory")
Console.WriteLine("Created directories.")
isoStore.CreateFile("InTheRoot.txt")
Console.WriteLine("Created a new file in the root.")
isoStore.CreateFile("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt")
Console.WriteLine("Created a new file in the InsideDirectory.")
End Using
End Sub
End Module
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Documentation
How to: Read and Write to Files in Isolated Storage - .NET
Learn more about: How to: Read and Write to Files in Isolated Storage
Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.