This is the second short article on the series "WP7 isolated storage series", focusing on real and practical examples with source code, rather than the theory of inventory. Next I will discuss how to create folders and files in Windows Phone 7 isolated storage.
·
WP7 isolated storage series-1. Introduction to isolated storage
·
WP7 isolated storage series-2. Create folders and files
·
WP7 isolated storage series-3. Use isolatedstoragesettings to store data
·
WP7 isolated storage series-4. Read and store text files
·
WP7 isolated storage series-5. Use xmlserializer to read and store XML files
·
WP7 isolated storage series-6. Use xmlwriter to read and store XML files
·
WP7 isolated storage series-7. Reading and storing images
·
WP7 isolated storage series-8. Read and store captured images
·
WP7 isolated storage series-9. Read and store binary files
·
WP7 isolated storage series-10. File Operations
·
WP7 isolated storage series-11. Suggestions and best practices
·
WP7 isolated storage series-12. open source database and help library files
Let's start by creating a simple Windows Phone 7 project. Next, add the following namespaces to mainpage. XAML. CS (or you can use this code on another page ):
using System.IO;using System.IO.IsolatedStorage;
Folder in WP7 isolated storage
·Create a folder
To create a folder named "new folder", we only need to call the createdirectory method of isolatedstoragefile:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); myIsolatedStorage.CreateDirectory("NewFolder");
Note: You can also create a compositing path folder other than "new folder", such as "folder1/folder2/folder3/newfolder ".
myIsolatedStorage.CreateDirectory("Folder1/Folder2/Folder3/NewFolder");
·Delete folder
To delete a folder, you only need to call the isolatedstoragefile method deletedirectory:
myIsolatedStorage.DeleteDirectory("NewFolder");
·Best practices
-Always check whether this directory exists
myIsolatedStorage.DirectoryExists(directoryName)
-When using isolated storage, try {} catch {} is always used to prevent users from seeing unexpected exceptions. The following method creates a folder using the parameter folder Name:
public void CreateDirectory(string directoryName){try{IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();if(!string.IsNullOrEmpty(directoryName) && !myIsolatedStorage.DirectoryExists(directoryName)){myIsolatedStorage.CreateDirectory(directoryName);}}catch (Exception ex){// handle the exception}}
To create a folder, you only need to call this method and assign the required parameters:
this.CreateDirectory("NewFolder");
-You can also use the same check to delete folders:
Note: The folder must be empty before it is deleted. Once deleted, the folder cannot be restored.
public void DeleteDirectory(string directoryName){try{IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();if (!string.IsNullOrEmpty(directoryName) && myIsolatedStorage.DirectoryExists(directoryName)){myIsolatedStorage.DeleteDirectory(directoryName);}}catch (Exception ex){// handle the exception}}
To delete a folder, you only need to call this method:
this.DeleteDirectory("NewFolder");
Note: Currently, an isolated storage cannot be used to rename a folder.
Files in WP7 isolated storage
·Create a file
To create a file named “somefile.txt, you only need to use streamwriter:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("NewFolder\\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage));
Note: If you do not specify any folder, the file will be created under the root directory. You can also use isolatedstoragefilestream to create files:
IsolatedStorageFileStream stream1 = new IsolatedStorageFileStream("SomeTextFile.txt", FileMode.Create, myIsolatedStorage);
·Delete an object
To delete a file from isolated storage, you only need to call the deletefile method of isolatedstoragefile:
myIsolatedStorage.DeleteFile("NewFolder/SomeFile.txt");
·Best practices
-Always check whether the folder where the file to be created exists
myIsolatedStorage.DirectoryExists(directoryName)
-Always check whether the file we are about to create exists. If yes, you must Delete the existing file and create a new one.
myIsolatedStorage.FileExists(filePath)
-When using isolated storage, try {} catch {} is always used to prevent users from seeing unexpected exceptions. The following method creates a folder using the parameter folder Name:
try{IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();StreamWriter writeFile;if (!myIsolatedStorage.DirectoryExists("NewFolder")){myIsolatedStorage.CreateDirectory("NewFolder");writeFile = new StreamWriter(new IsolatedStorageFileStream("NewFolder\\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage));}else{writeFile = new StreamWriter(new IsolatedStorageFileStream("NewFolder\\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage));}}catch (Exception ex){// do something with exception}
Note: to completely delete all data in isolated storage, you only need to call store. Remove (). Here, "Store" is an instance of isolatedstoragefile.
In this article, I discussed how to create folders and files in WP7 isolated storage. We look forward to the following articles.
Link: http://www.windowsphonegeek.com/tips/all-about-wp7-isolated-storage-files-and-folders