This article has been
excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors
of C# Corner.
The .NET Framework provides the IsolatedStorageFile class to represent a store
for an assembly.
Listing 22.34 shows various aspects of IsolatedStorage namespace classes and
methods.
Listing 22.34: IsolatedStorage Example
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.IO.IsolatedStorage;
using System.IO;
namespace
IsolatedStorageFile_Class
{
class Program
{
static void
Main(string[] args)
{
// isolated storage
// in System.IO.IsolatedStorage
namespace
// Obtain a store
IsolatedStorageFile store1
=
IsolatedStorageFile.GetUserStoreForDomain();
// you can also use static GetStore
methods like below
/*
IsolatedStorageFile storefile1 = IsolatedStorageFile.GetStore(
IsolatedStorageScope.User |
IsolatedStorageScope.Assembly,
null,
null);
IsolatedStorageFile storefile1 = IsolatedStorageFile.GetStore(
IsolatedStorageScope.User |
IsolatedStorageScope.Assembly |
IsolatedStorageScope.Domain,
null,
null);
IsolatedStorageFile storefile1 = IsolatedStorageFile.GetStore(
IsolatedStorageScope.User |
IsolatedStorageScope.Assembly |
IsolatedStorageScope.Roaming,
null,
null);
IsolatedStorageFile storefile1 = IsolatedStorageFile.GetStore(
IsolatedStorageScope.User |
* IsolatedStorageScope.Assembly |
IsolatedStorageScope.Domain |
IsolatedStorageScope.Roaming,
null,
null);
*/
IsolatedStorageFileStream stream1
=
new
IsolatedStorageFileStream("file1.txt",
FileMode.Create, store1);
StreamWriter writer =
new StreamWriter(stream1);
writer.WriteLine("Hello Isolated
Storage");
writer.Close();
stream1.Close();
store1.Close();
// Read from a file in isolated storage
IsolatedStorageFileStream
stream2 =
new
IsolatedStorageFileStream("file2.txt",
FileMode.Open, store1);
StreamReader reader =
new StreamReader(stream1);
String sb = reader.ReadToEnd();
reader.Close();
stream1.Close();
store1.Close();
// Obtain a store
// Create a directory
store1.CreateDirectory("dir1");
// Create two directories, one inside the
other
store1.CreateDirectory("dir2/dir3");
// Create a directory
store1.CreateDirectory("dir4");
// Delete the directory
store1.DeleteDirectory("dir4");
// Obtain a store
IsolatedStorageFile store2
=
IsolatedStorageFile.GetUserStoreForDomain();
// Create an empty file
IsolatedStorageFileStream
stream3 =
new
IsolatedStorageFileStream("file3.txt",
FileMode.Create, store1);
stream1.Close();
// Delete the file
store1.DeleteFile("file3.txt");
// Find a storage space available
// Obtain a store
IsolatedStorageFile store3
=
IsolatedStorageFile.GetUserStoreForDomain();
// Compute the storage available in the
store
ulong spaceLeft =
store1.MaximumSize - store1.CurrentSize;
// Enumerate files and directories
// Obtain a store
IsolatedStorageFile store4
=
IsolatedStorageFile.GetUserStoreForDomain();
// Get all of the directories in the root
of the store
string[] directories =
store1.GetDirectoryNames("*");
// Get all of the files in the root of the
store
string[] files =
store1.GetFileNames("*");
}
}
}
Conclusion
Hope this article would have helped you in understanding
IsolatedStorageFile Class in .NET. See other articles on the website on .NET and C#.



Osman GumusPosted Nov 19, 2010, 5:02 AM EST
I guess there a few mistakes such as while creating and deleting file3.txt u used store1 instead of store2 or I didn't understand. And deleting file3.txt is not possible. Could u send this code again?
justitPosted Mar 26, 2010, 12:06 AM EDT
ds lite dsi card nintendo dsi TopToy DSTTi Multimedia Card for NDSi/NDS/NDS Lite (Supports SDHC)-Genuine NDSTT.COM Ver Acekard 2i AK2i Multimedia SDHC ds dstt ds tt card dstti Cards r4i r4 r4 adapter r4 card r4i card ez flash nds console acekard 2 ak2i dsi 1.4 acekard AK2i m3i zero for dsi ds m3