WP7 independent storage IsolatedStorageFile file read and write file implementation program

Source: Internet
Author: User
Tags foreach
The code is as follows: Copy code
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile. GetUserStoreForApplication ();
// If it is a rewrite file, it is best to delete the file before writing. Otherwise, if the size of the file written this time is smaller than the size of the file itself, the data of the previous file still exists, A problem occurs during reading.
If (myIsolatedStorage. FileExists (filename) = true)
                {
MyIsolatedStorage. DeleteFile (filename );
                }
Using (IsolatedStorageFileStream stream = myIsolatedStorage. OpenFile (filename, FileMode. CreateNew ))
                {
List <VideoItemData> favlist = new List <VideoItemData> ();
Foreach (var I in coll)
                    {
Favlist. Add (I );
                    }
Using (var str = new StreamWriter (stream ))
                    {
Str. Write (JsonConvert. SerializeObject (favlist ));
                    }
                }

Exercise caution when commenting on the comments in the code. It is best to delete the file before re-writing. Otherwise, errors may occur when reading data due to incorrect data. It is difficult to troubleshoot such errors.

The code for reading the file is as follows:

The code is as follows: Copy code

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile. GetUserStoreForApplication ();
If (myIsolatedStorage. FileExists ("fav. json") = true)
                {
// Open the file
IsolatedStorageFileStream stream = myIsolatedStorage. OpenFile ("fav. json", FileMode. Open );
Try
                    {
Using (var sr = new StreamReader (stream ))
                        {
// Read the file
String result = sr. ReadToEnd ();
Result = result. Trim ();
Sr. Close ();
// Deserialization as an object
List <VideoItemData> Data = (List <VideoItemData>) JsonConvert. DeserializeObject <List <VideoItemData> (result );
Foreach (var d in Data)
                            {
// Add to listview data source
FavData. Add (d );
                            }
                        }
                    }
Catch (Exception ex)
                    {
MessageBox. Show (ex. Message );
                    }
Finally
                    {
Stream. Close ();
                    }
                }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.