1. WP7 file operations:
If a file is written to wp7, we will use:IsolatedStorageFile
The Code is as follows:
① Write files
private void WriteFile(string fileName, string content){ using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.CreateFile(fileName)) { using (StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream)) { streamWriter.Write(content); } } }}
② Reading files
private string ReadFile(string fileName){ string text; using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.OpenFile(fileName, FileMode.Open)) { using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream)) { text = streamReader.ReadToEnd(); } } } return text;}
Ii. WP8 File Operations
Wp8 and win8 file operations are similar. If you have written file operations under win8, WP8 is naturally familiar. Two interfaces are required: IStorageFile and IStorageFolder, one is file operations and the other is directory operations.
Both interfaces are in the Windwos. Storage component,
Note: WinRT asynchronous programming is widely used in windows 8 Development, so is programming in WP8.
The Code is as follows:
① Write files:
public async Task WriteFile(string fileName, string text){ IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (Stream stream = await storageFile.OpenStreamForWriteAsync()) { byte[] content = Encoding.UTF8.GetBytes(text); await stream.WriteAsync(content, 0, content.Length); }}
② Reading files:
public async Task<string> ReadFile(string fileName){ string text; IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName); IRandomAccessStream accessStream = await storageFile.OpenReadAsync(); using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size)) { byte[] content = new byte[stream.Length]; await stream.ReadAsync(content, 0, (int) stream.Length); text = Encoding.UTF8.GetString(content, 0, content.Length); } return text;}
Iii. compatibility issues
The code above briefly describes the file write and read operations on the WP7 and WP8 platforms respectively.
So the definition of the file directory structure in the WP7 application is the same in WP8?
In fact, the two are a little different. This difference is also one of the elements that must be considered during the migration from WP7 to WP8 in the future.
Create the test.txt file in wp7. the file directory structure in the isolated storage area is as follows:
C: \ Data \ Users \ defaappappaccount \ AppData \ {ProductID} \ Local \ IsolatedStore\ Test.txt
In WP8, create the same file. The file storage directory structure is as follows:
C: \ Data \ Users \ DefaultAppAccount \ AppData \ {ProductID} \ Local \ test. Txt
We found that in WP8 development, if you do not want to change the original file directory structure when using the existing WP7 code, you must first obtain the WP7 isolated storage directory. that is:
IStorageFolder applicationFolder =await ApplicationData.Current.LocalFolder.GetFolderAsync("IsolatedStore");
Finally, we want to explain the following:
If you want to be "lazy" or reduce development workload, let's take a look at what I mentioned above.
However, if you plan to build two WP8/WIN8 platforms, you 'd better develop a new WP8-based application, which can speed up the porting of your Win8 application.
Source:Http://jasonwei.com/archives/463