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 (); } } |