The storage mode of files in Windows Phone7 is independent, that is, isolated storage space (IsolatedStorage). For folder and file operations, you need to use the IsolatedStorageFile class. IsolatedStorageFile provides IO operations such as spatial acquisition of isolated storage, deletion, movement of folders, creation and deletion of files. The operation of files in Windows Phone7 requires the introduction of namespaces System.IO.IsolatedStorage and System.IO.
The following categories are available under the System.IO.IsolatedStorage namespace: (Learn more: Https://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage %28vs.95%29.aspx)
The 1.IsolatedStorageFile class represents an isolated storage area that contains files and folders for manipulating isolated storage spaces folders and files.
The 2.IsolatedStorageFileStream class represents exposing files in isolated storage for reading and writing file streams in isolated storage spaces.
The 3.IsolatedStorageSettings class provides a dictionary<tkey that stores key/value pairs in isolated storage, tvalue>, and Dictionary for storing configuration information for an application.
The 4.IsolatedStorageException class is used to detect exceptions that are thrown when an operation in isolated storage fails.
There are generally several steps to working with files in Windows Phone7:
1. First introduce namespaces System.IO.IsolatedStorage and System.IO;
2. Get the isolated storage space of the application, call the static method Getuserstoreforapplication () to return the IsolatedStorageFile object;
3. Use the method provided by the obtained stand-alone space object for IO operations (if a file stream operation is involved, the file stream should be closed after the file stream operation is completed);
4. Catch an exception to the file operation.
folder and file operations the operations on folders and files are based on IsolatedStorageFile class objects, which are commonly used as follows:
CopyFile (String): Copies an existing file to a new file.
CopyFile (String, String, Boolean): Copies an existing file to a new file, and can overwrite an existing file.
CreateDirectory: Create a directory in isolated storage scope.
CreateFile: Create a file in isolated storage.
DeleteDirectory: Deletes the directory in the isolated storage scope.
DeleteFile: Deletes files in isolated storage.
DirectoryExists: Checks whether the specified path refers to an existing directory in the isolated store.
FileExists: Checks whether the specified path refers to an existing file in the isolated store.
MoveDirectory: Moves the specified directory and its contents to a new location.
MoveFile: Moves the specified file to a new location and allows you to specify a new file name.
OpenFile (String, FileMode): Opens the file in the specified mode.
OpenFile (String, FileMode, FileAccess): Opens the file in the specified mode with the specified file access permission.
The operation is slightly more complex when writing to a file. The write of the file is written as a stream, and the file is opened with the IsolatedStorageFileStream file stream operation class provided by IsolatedStorage first. Then use the StreamWriter class to write the open file to the object, and finally close the file stream.
The read operation of the file and the writing steps of the file are basically the same, the StreamReader class is used for reading, and finally the file stream needs to be closed.
Here's an example of how to implement a folder and file operation
Folder operations:
MainPage.xaml.cs Main code
1 //Create a folder2 voidCreatebutton_click (Objectsender, RoutedEventArgs e)3 {4 using(IsolatedStorageFile file =isolatedstoragefile.getuserstoreforapplication ())5 {6 file. CreateDirectory (dir);7 }8 }9 Ten One //Search Folder A voidExistsbutton_click (Objectsender, RoutedEventArgs e) - { - using(IsolatedStorageFile file =isolatedstoragefile.getuserstoreforapplication ()) the { - if(file. DirectoryExists (dir)) - { -MessageBox.Show ("folder exists! "); + } - Else + { AMessageBox.Show ("folder does not exist! "); at } - } - - - //Delete a folder - voidDeletebutton_click (Objectsender, RoutedEventArgs e) in { - using(IsolatedStorageFile file =isolatedstoragefile.getuserstoreforapplication ()) to { + file. DeleteDirectory (dir); - } the}
View Code
File operation:
MainPage.xaml.cs
1 //New File2 voidNewbutton_click (Objectsender, RoutedEventArgs e)3 { 4 using(IsolatedStorageFile file =isolatedstoragefile.getuserstoreforapplication ())5 {6IsolatedStorageFileStream filestream= file. CreateFile (TextBox.Text +". txt");7 //Close File Stream8 Filestream.close (); 9 }Ten } One //Check File A voidCheckbutton_click (Objectsender, RoutedEventArgs e) - { - using(IsolatedStorageFile file =isolatedstoragefile.getuserstoreforapplication ()) the { - if(file. FileExists (TextBox.Text +". txt")) - { -MessageBox.Show ("file already exists"); + } - Else + { AMessageBox.Show ("file does not exist"); at } - - } - } - - //Write File in voidWritebutton_click (Objectsender, RoutedEventArgs e) - { to Try + { - using(IsolatedStorageFile file =isolatedstoragefile.getuserstoreforapplication ()) the { * //Open File $ using(IsolatedStorageFileStream FileStream = file.) OpenFile (Filenametextbox.text +". txt", FileMode.Open, FileAccess.Write))Panax Notoginseng { - //instantiate the StreamWriter class theStreamWriter StreamWriter =NewStreamWriter (FileStream); + //Use the WriteLine method with the A Streamwriter.writeline (contenttextbox.text); the //need to close after write completion + streamwriter.close (); - } $ } $ - } - Catch(IsolatedStorageException ex) the { - MessageBox.Show (ex. ToString ());Wuyi the } - } Wu - About //Read File $ voidReadfilepage_loaded (Objectsender, RoutedEventArgs e) - { - using(IsolatedStorageFile file =isolatedstoragefile.getuserstoreforapplication ()) - { A if(file. FileExists (navigationcontext.querystring["file"]. ToString ())) + { the //Open File - using(IsolatedStorageFileStream FileStream = file.) OpenFile (navigationcontext.querystring["file"]. ToString (), FileMode.Open, FileAccess.ReadWrite)) $ { theStreamReader StreamReader =NewStreamReader (FileStream);//instantiate the StreamReader class the This. Contenttextblock.text = StreamReader.ReadLine ();//reading content using the ReadToEnd () method theStreamreader.close ();//Close File Stream the } - } in Else the { theMessageBox.Show (navigationcontext.querystring["file"]. ToString () +"file does not exist"); About } the } the}
View Code
Windows Phone Development folder and file Operations series (i) folder and file operations