Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

File manipulation with IsolatedStorageFile on Windows Phone

From Wiki
Jump to: navigation, search
somnathbanik (Talk | contribs)
(Somnathbanik -)
pavan.pareta (Talk | contribs)
(Pavan.pareta - - Implementation)
 
(25 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:Windows Phone]][[Category:Code Snippet]][[Category:Code Examples]]
+
[[Category:Files/Data on Windows Phone]][[Category:Windows Phone 7.5]][[Category:Windows Phone 8]][[Category:XAML]][[Category:Code Examples]]
{{Abstract|This article demonstrates how to store data locally on  WP7  using Isolated Storage.}}
+
[[Category:Files/Data on Windows Phone]][[Category:Windows Phone 7.5]][[Category:Windows Phone 8]][[Category:XAML]][[Category:Code Examples]]
{{ArticleMetaData
+
{{Abstract|This code example demonstrates how to store files locally using Windows Phone Isolated Storage.}} Official documentation on the topic(s) is available here: [http://msdn.microsoft.com/en-us/library/ff626522(v=vs.92).aspx Local Data Storage for Windows Phone] (MSDN).
|sourcecode= [[Media: IsolatedStorageWP7.zip]]<!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
{{Tip|While {{Icode|IsolatedStorageFile}} works in Windows Phone 7 and 8, if you're focussing on Windows Phone 8 only you should use the new API - see [http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681698(v&#61;vs.105).aspx Quickstart: Working with files and folders in Windows Phone 8]}}
 +
{{SeeAlso|
 +
* [http://msdn.microsoft.com/en-us/library/ff626522(v&#61;vs.92).aspx Local Data Storage for Windows Phone] (MSDN)
 +
* [[Introduction and best practices for IsolatedStorageSettings]]
 +
* [http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681698(v&#61;vs.105).aspx Quickstart: Working with files and folders in Windows Phone 8]}}
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= [[Media: IsolatedStorageWP7.zip]]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= WP7 Emulator  <!-- Devices tested against - e.g. ''devices=N95, N8'') -->
+
|devices= Nokia Lumia 820, Lumia 920
|sdk= [http://www.developer.nokia.com/Develop/Windows_Phone/Tools/ Windows Phone SDK 7.1])
+
|sdk= [http://www.microsoft.com/en-us/download/details.aspx?id=35471 Windows Phone SDK 8.0], Windows Phone SDK 7.1
|platform= WP7.1 <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
+
|platform= Windows Phone 8.0/ 7.1
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= File Handling/ Isolated Storage <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|keywords= File Handling/ Isolated Storage <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
|id= <!-- Article Id (Knowledge base articles only) -->
+
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
|creationdate= 18th November,2011<!-- Format YYYYMMDD -->
+
|translated-by= <!-- [[User:XXXX]] -->
|author= [[User:somnathbanik]] <!-- Display as link [[User:username]] -->
+
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20111118
 +
|author= [[User:Somnathbanik]]
 
}}
 
}}
 
==Introduction==
 
==Introduction==
Isolated Storage is used to store local data on a Windows Phone 7. There are two ways we can store data in Isolated Storage :
+
Windows Phone 7 apps store their local data in an app-specific file system area known as Isolated Storage. There are three ways we can store data:
* [http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx IsolatedStorageFile] :  It allows to store  Files on the user’s device.
+
* [http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx IsolatedStorageFile] - stores files
* [http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragesettings%28v=VS.95%29.aspx IsolatedStorageSettings] :  It allows to store name/value pairs in a dictionary that we can retrieve later using library. 
+
* [http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragesettings%28v=VS.95%29.aspx IsolatedStorageSettings] - stores settings as name/value pairs in a dictionary
+
* [http://msdn.microsoft.com/en-us/library/hh202865(v=vs.92).aspx LINQ to SQL] - store relational data in a local database.
[[File: IsolatedStorageWP7.png|thumb|301px|none| File Manipulation in WP7  ]]
+
{{Note|Symbian developers will recognise that Isolated Storage is conceptually similar to an app's private data cage. The main difference is that Windows Phone apps only have access to their own isolated storage - there is no ''shared file system''. This creates challenges for apps that want to share data or files.}}
[[File: IsolatedStorageWP7Explorer.png|thumb|101px|none| WP7 Isolated Storage Explorer  ]]
+
This code examples uses [http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream%28v=VS.90%29.aspx IsolatedStorageFileStream] class to write and retrieve text (provided by the user) to a file. In a following section, we also provide information about [[#Windows Phone 7 Isolated Storage Explorer|Isolated Storage Explorer]], a tool for exploring the file structure in isolated storage.  
+
[[File: IsolatedStorageWP7.png|thumb|301px|none| File Manipulation in WP7]]
==Basic Idea==
+
 
In this article we  will use [http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream%28v=VS.90%29.aspx IsolatedStorageFileStream] class to Read/Write/Create file in Isolated Storage. For our simplicity we will create  an application that can take  user data as input and save the data in a file in Isolated Storage, and also can retrieve the data from Isolated Storage.  
+
+
 
==Implementation ==
 
==Implementation ==
When we are ready with an empty project for WP7, let’s take some data from user input and save it to a file in Isolated Storage.
+
When we are ready with an empty project for Windows Phone, let’s take some data from user input and save it to a file in Isolated Storage.
<code cpp>
+
<code csharp>
 
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
 
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
            StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("TestFile.txt", FileMode.OpenOrCreate, fileStorage));
+
StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("TestFile.txt", FileMode.OpenOrCreate, fileStorage));
            Writer.WriteLine(textBox1.Text);
+
Writer.WriteLine(textBox1.Text);
            Writer.Close();
+
Writer.Close();
       
+
 
</code>
 
</code>
We have created an object of  [http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx IsolatedStorageFile] which obtain a virtual storage for the application. Then let’s create a new [http://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=VS.90%29.aspx StreamWriter], to write the file to the specified location. {{Icode|writeLine()}} is used to write the content to the file. And Finally we close the [http://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=VS.90%29.aspx StreamWriter]. This will just create a new file with the name ''TestFile.txt'' in the Isolated Storage.  
+
We have created an object of  [http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx IsolatedStorageFile] which obtain a virtual storage for the application. Then let’s create a new [http://msdn.microsoft.com/en-us/library/system.io.streamwriter(v=VS.95).aspx StreamWriter], to write the file to the specified location. {{Icode|WriteLine()}} is used to write the content to the file. And Finally we close the [http://msdn.microsoft.com/en-us/library/system.io.streamwriter(v=VS.95).aspx StreamWriter]. This will just create a new file with the name '''TestFile.txt''' in the Isolated Storage.  
+
To retrieve the content of the file from the Isolated Storage. Let’s again create a virtual store for the application. This time we will create a [http://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=VS.90%29.aspx StreamReader] to read the content.
+
<code cpp>
+
 
+
            IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
+
            StreamReader Reader = null;
+
            try
+
            {
+
 
+
                Reader = new StreamReader(new IsolatedStorageFileStream("TestFile.txt", FileMode.Open, fileStorage));
+
                string textFile = Reader.ReadToEnd();
+
                textBox2.Text = textFile;
+
                Reader.Close();
+
            }
+
            catch
+
            {
+
                MessageBox.Show("File it not created");
+
            }
+
 
   
 
   
 +
To retrieve the content of the file from the Isolated Storage. Let’s again create a virtual store for the application. This time we will create a [http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=VS.95).aspx StreamReader] to read the content.
 +
<code csharp>
 +
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
 +
StreamReader Reader = null;
 +
try
 +
    {
 +
    Reader = new StreamReader(new IsolatedStorageFileStream("TestFile.txt", FileMode.Open, fileStorage));
 +
    string textFile = Reader.ReadToEnd();
 +
    textBox2.Text = textFile;
 +
    Reader.Close();
 +
    }
 +
catch
 +
    {
 +
    MessageBox.Show("File it not created");
 +
    }
 
</code>
 
</code>
Then read the file from the specific location. Read the content of the file with {{Icode|ReadToEnd()}} and finally display the content of the file to the Textbox. If the file doesn’t exist and user clicks on the retrieve button to display the file content then we display a message. We can also check the file existence with {{Icode|IsolatedStorageFile::FileExists(FilePath)}}.
+
Then read the file from the specific location. Read the content of the file with {{Icode|ReadToEnd()}} and finally display the content of the file to the Textbox. If the file doesn’t exist and user clicks on the retrieve button to display the file content then we display a message. We can also check the file existence with {{Icode|IsolatedStorageFile::FileExists(FilePath)}}.
+
 
 
==Windows Phone 7 Isolated Storage Explorer==
 
==Windows Phone 7 Isolated Storage Explorer==
Windows Phone 7 Isolated Storage Explorer allows you to see and explore files in the Isolated Storage. You can also import/export the file to/from the Isolated Storage.  
+
[[File: IsolatedStorageWP7Explorer.png|thumb|301px|right|WP7 Isolated Storage Explorer ]]
 +
Windows Phone 7 Isolated Storage Explorer allows you to see and explore files in the Isolated Storage. You can also import/export the file to/from the Isolated Storage.  
 
*First let’s download and install the [http://wp7explorer.codeplex.com/releases/view/61282 Windows Phone 7 Isolated Storage Explorer] installation file from CodePlex .  
 
*First let’s download and install the [http://wp7explorer.codeplex.com/releases/view/61282 Windows Phone 7 Isolated Storage Explorer] installation file from CodePlex .  
 
*Add the reference '''IsolatedStorageExplorer.dll''' to the project from  
 
*Add the reference '''IsolatedStorageExplorer.dll''' to the project from  
{{Icode|C:\Program Files\WP7 Isolated Storage Explorer\Library\IsolatedStorageExplorer.dll}}
+
''C:\Program Files\WP7 Isolated Storage Explorer\Library\IsolatedStorageExplorer.dll''
*Start IsolatedStorageExplorer in the ''App.xaml.cs'' while launching.  
+
*Start ''IsolatedStorageExplorer'' in the '''App.xaml.cs''' while launching.  
<code cpp>
+
<code csharp>
 
private void Application_Launching(object sender, LaunchingEventArgs e)
 
private void Application_Launching(object sender, LaunchingEventArgs e)
        {
+
    {
            IsolatedStorageExplorer.Explorer.Start("localhost");
+
    IsolatedStorageExplorer.Explorer.Start("localhost");
        }
+
    }
 
   
 
   
 
</code>
 
</code>
 
''Localhost'' is the name/IP address of the system
 
''Localhost'' is the name/IP address of the system
*In ''Activated'' event in'' App.xaml.cs'' call  {{Icode|IsolatedStorageExplorer.Explorer.RestoreFromTombstone()}}  
+
*In ''Activated'' event in '''App.xaml.cs''' call  {{Icode|IsolatedStorageExplorer.Explorer.RestoreFromTombstone()}}  
<code cpp>
+
<code csharp>
+
private void Application_Activated(object sender, ActivatedEventArgs e)
  private void Application_Activated(object sender, ActivatedEventArgs e)
+
    {
        {
+
    IsolatedStorageExplorer.Explorer.RestoreFromTombstone();
            IsolatedStorageExplorer.Explorer.RestoreFromTombstone();
+
    }
        }
+
+
 
</code>
 
</code>
*Run the application and launch the IsolatedStorageExplorer from  
+
*Run the application and launch the IsolatedStorageExplorer from  
 
''Start->All Programs-> WP7 Isolated Storage Explorer
 
''Start->All Programs-> WP7 Isolated Storage Explorer
 
''
 
''
*This will display the file being created, you can download the file to the desktop, delete the file or even can import file to the Isolated Storage.
+
*This will display the file being created, you can download the file to the desktop, delete the file or even can import file to the Isolated Storage.
+
 
 
==Summary==
 
==Summary==
When we create a file in Isolated Storage the I/O operations are restricted to, so the data are local to the application. It doesn’t have direct access from any other file system or application.  So it’s secure from unauthorized access. If you are sharing same data from two applications then those data can’t be local to any of the application, it suggested to store the data in cloud. The storage resources on a phone are limited, so it is recommended to store necessary data.
+
When we create a file in Isolated Storage the I/O operations are restricted to, so the data are local to the application. It doesn’t have direct access from any other file system or application.  So it’s secure from unauthorized access. If you are sharing same data from two applications then those data can’t be local to any of the application, it suggested to store the data in cloud. The storage resources on a phone are limited, so it is recommended to store necessary data.
+
 
   
 
   
 
==Source Code==
 
==Source Code==
 
The full source code of the example is available here: [[File: IsolatedStorageWP7.zip]]
 
The full source code of the example is available here: [[File: IsolatedStorageWP7.zip]]
 +
<!-- Translation --> [[zh-hans:WP7 Isolated Storage中的文件管理]]

Latest revision as of 16:23, 3 August 2013

This code example demonstrates how to store files locally using Windows Phone Isolated Storage. Official documentation on the topic(s) is available here: Local Data Storage for Windows Phone (MSDN).

Tip.pngTip: While IsolatedStorageFile works in Windows Phone 7 and 8, if you're focussing on Windows Phone 8 only you should use the new API - see Quickstart: Working with files and folders in Windows Phone 8

WP Metro Icon File.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone SDK 8.0, Windows Phone SDK 7.1
Devices(s): Nokia Lumia 820, Lumia 920
Compatibility
Platform(s): Windows Phone 8.0/ 7.1
Windows Phone 8
Windows Phone 7.5
Article
Keywords: File Handling/ Isolated Storage
Created: somnathbanik (18 Nov 2011)
Last edited: pavan.pareta (03 Aug 2013)

Contents

[edit] Introduction

Windows Phone 7 apps store their local data in an app-specific file system area known as Isolated Storage. There are three ways we can store data:

Note.pngNote: Symbian developers will recognise that Isolated Storage is conceptually similar to an app's private data cage. The main difference is that Windows Phone apps only have access to their own isolated storage - there is no shared file system. This creates challenges for apps that want to share data or files.

This code examples uses IsolatedStorageFileStream class to write and retrieve text (provided by the user) to a file. In a following section, we also provide information about Isolated Storage Explorer, a tool for exploring the file structure in isolated storage.

File Manipulation in WP7

[edit] Implementation

When we are ready with an empty project for Windows Phone, let’s take some data from user input and save it to a file in Isolated Storage.

IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("TestFile.txt", FileMode.OpenOrCreate, fileStorage));
Writer.WriteLine(textBox1.Text);
Writer.Close();

We have created an object of IsolatedStorageFile which obtain a virtual storage for the application. Then let’s create a new StreamWriter, to write the file to the specified location. WriteLine() is used to write the content to the file. And Finally we close the StreamWriter. This will just create a new file with the name TestFile.txt in the Isolated Storage.

To retrieve the content of the file from the Isolated Storage. Let’s again create a virtual store for the application. This time we will create a StreamReader to read the content.

IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader Reader = null;
try
{
Reader = new StreamReader(new IsolatedStorageFileStream("TestFile.txt", FileMode.Open, fileStorage));
string textFile = Reader.ReadToEnd();
textBox2.Text = textFile;
Reader.Close();
}
catch
{
MessageBox.Show("File it not created");
}

Then read the file from the specific location. Read the content of the file with ReadToEnd() and finally display the content of the file to the Textbox. If the file doesn’t exist and user clicks on the retrieve button to display the file content then we display a message. We can also check the file existence with IsolatedStorageFile::FileExists(FilePath).

[edit] Windows Phone 7 Isolated Storage Explorer

WP7 Isolated Storage Explorer

Windows Phone 7 Isolated Storage Explorer allows you to see and explore files in the Isolated Storage. You can also import/export the file to/from the Isolated Storage.

C:\Program Files\WP7 Isolated Storage Explorer\Library\IsolatedStorageExplorer.dll

  • Start IsolatedStorageExplorer in the App.xaml.cs while launching.
private void Application_Launching(object sender, LaunchingEventArgs e)
{
IsolatedStorageExplorer.Explorer.Start("localhost");
}

Localhost is the name/IP address of the system

  • In Activated event in App.xaml.cs call IsolatedStorageExplorer.Explorer.RestoreFromTombstone()
private void Application_Activated(object sender, ActivatedEventArgs e)
{
IsolatedStorageExplorer.Explorer.RestoreFromTombstone();
}
  • Run the application and launch the IsolatedStorageExplorer from

Start->All Programs-> WP7 Isolated Storage Explorer

  • This will display the file being created, you can download the file to the desktop, delete the file or even can import file to the Isolated Storage.

[edit] Summary

When we create a file in Isolated Storage the I/O operations are restricted to, so the data are local to the application. It doesn’t have direct access from any other file system or application. So it’s secure from unauthorized access. If you are sharing same data from two applications then those data can’t be local to any of the application, it suggested to store the data in cloud. The storage resources on a phone are limited, so it is recommended to store necessary data.

[edit] Source Code

The full source code of the example is available here: File:IsolatedStorageWP7.zip

This page was last modified on 3 August 2013, at 16:23.
813 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×