To be consistent with Windows 8, Windows Phone 8 provides a new file operation type, which is included in windows. storage namespace, including storagefolder, storagefile, fileio, and other class libraries. The windwos. Storage component mainly contains istoragefile and istoragefolder. We can see that one is file operations and the other is folder operations.
In the official SDK
Isolatedstoragefile. getuserstoreforapplication (); view the private variable m_appfilespathaccess.
And Windows Phone 8 through
await ApplicationData.Current.LocalFolder
The obtained file directory structure definitions are consistent. All
C: \ data \ Users \ defapps \ appdata \ {productid} \ Local
The difference is that WP8 basically uses the asynchronous architecture for file operations, while WP7 mainly uses the synchronous method.
Use istoragefile to create a file
public void IStorageFileCreateFile(string fileName)
{
IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
IAsyncOperation<StorageFile> iaStorageFile = applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
iaStorageFile.Completed = (IAsyncOperation<StorageFile> asyncInfo, AsyncStatus asyncStatus) =>
{
if (asyncStatus == AsyncStatus.Completed)
{
}
};
}
Use istoragefile to read an object
public async Task<string> IStorageFileReadFile(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;
}