IsolatedStorageFile indicates an independent storage zone that contains files and directories. IsolatedStorageFile allows you to create real files and directories on your devices.
This class abstracts virtual file systems that are stored independently. The IsolatedStorageFile object corresponds to a specific independent storage range, in which there is a file represented by the IsolatedStorageFileStream object. Applications can use independent storage to store data in the independent parts of the data in the file system, without specifying a specific path in the file system.
The root of the Virtual File System is located in the fuzzy folder of each user on the physical file system. Each unique identifier provided by the host is mapped to a different root that provides each application with its own virtual file system. An application cannot navigate from its own file system to another file system.
Because the independent storage zone is within the scope of a specific assembly, therefore, most other managed code cannot access the data of your code (highly trusted managed code and management tools can access the storage zone from other assembly ). Unmanaged code can access any independent storage area.
1. Obtain the IsolatedStorageFile object of the independent storage.
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile. GetUserStoreForApplication ();
2. Create a directory
MyIsolatedStorage. CreateDirectory (path );
3. Create a file
MyIsolatedStorage. CreateFile (path );
Note: If the folder where the file is created does not exist, the file cannot be created successfully.
4. Determine whether the directory exists
Bool isExists = myIsolatedStorage. DirectoryExists (path );
5. Determine whether the file exists
Bool isExists = myIsolatedStorage. FileExists (path );
6. delete an object
MyIsolatedStorage. DeleteFile (path );
7. delete a directory (recursive implementation is required)
View Code
/// <Summary>
/// Recursively delete a directory
/// </Summary>
/// <Param name = "path"> directory path </param>
/// <Param name = "myIsolatedStorage"> independent storage </param>
Private static void rmDirAll (String path, IsolatedStorageFile myIsolatedStorage)
{
String searchPattern = path + "\\*";
// Find all subdirectories in the directory and delete them recursively
String [] dirs = myIsolatedStorage. GetDirectoryNames (searchPattern );
For (int I = 0; I <dirs. Length; I ++)
{
String dPath = path + "\" + dirs [I];
RmDirAll (dPath, myIsolatedStorage );
}
// Search for all files in the directory and delete it
String [] files = myIsolatedStorage. GetFileNames (searchPattern );
For (int j = 0; j <files. Length; j ++)
{
String fPath = path + "\" + files [j];
MyIsolatedStorage. DeleteFile (fPath );
}
// This directory can be deleted only after all subdirectories and files under this directory are deleted.
MyIsolatedStorage. DeleteDirectory (path );
}
8. complex files
MyIsolatedStorage. CopyFile (srcFilename/* Source File */, dstFilename/* target file */, overwrite/* overwrite */);
9. Read files
View Code
If (myIsolatedStorage. FileExists (path ))
{
// Open the file
Using (IsolatedStorageFileStream fileStream = myIsolatedStorage. OpenFile (path, FileMode. Open, FileAccess. Read ))
{
Using (StreamReader read = new StreamReader (fileStream ))
{
String str = read. ReadToEnd ();
}
}
}
10. Write files
Using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream (path, fMode, myIsolatedStorage ))
{
FileStream. Write (data, 0, size );
}
11. Get the date when the file was created
DateTimeOffset offset = myIsolatedStorage. GetCreationTime (path );
DateTime creatDate = offset. DateTime;