metro File IO


Here are some code snips to replace the missing funcionality in System.IO
Extracted from the samples at http://code.msdn.microsoft.com/windowsapps


Common code:

using System.Threading.Tasks;
using Windows.Storage;
string filename = "sample.txt"
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;




Creating a file

StorageFile sampleFile = await storageFolder.CreateFileAsync(filename);

Writing to a file

string data = "some data"
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile sampleFile = await storageFolder.GetFileAsync(filename);
IRandomAccessStream writeStream = await sampleFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream outputStream = writeStream.GetOutputStreamAt(0);
DataWriter dataWriter = newDataWriter(outputStream);
dataWriter.WriteString(data);
await dataWriter.StoreAsync();
outputStream.FlushAsync().Start();

Reading from a file

StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile sampleFile = await storageFolder.GetFileAsync(filename);
IRandomAccessStream readStream = await sampleFile.OpenAsync(FileAccessMode.Read);
IInputStream inputStream = readStream.GetInputStreamAt(0);
DataReader dataReader = newDataReader(inputStream);
uint numBytesLoaded = await dataReader.LoadAsync((uint) readStream.Size);
string result = dataReader.ReadString(numBytesLoaded);


Accessing FIle Properties

StorageFile sampleFile = await storageFolder.GetFileAsync(filename);
BasicProperties basicProperties = await sampleFile.Properties.GetBasicPropertiesAsync();
string result = "Filename: " + sampleFile.FileName +
"\nFile type: " + sampleFile.FileType +
"\nFile size: " + basicProperties.Size + " bytes" +
"\nDate modified: " + basicProperties.DateModified;


Adding a file to the Most Recently Used list

StorageFile sampleFile = await storageFolder.GetFileAsync(filename);
token = StorageApplicationPermissions.MostRecentlyUsedList.Add(sampleFile);


Displaying the contents of the MRU list

AccessListEntryView entries = StorageApplicationPermissions.MostRecentlyUsedList.Entries;
StringBuilder outputText = newStringBuilder("The MRU list contains the following items:\n\n");

foreach (AccessListEntry entry in entries)
{
StorageFile storageFile = awaitStorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(entry.Token);
outputText.Append(storageFile.FileName + "\n");
}
string result = outputText.ToString();


Opening a file from the MRU list

StorageFile sampleFile = awaitStorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token);
IRandomAccessStream readStream = await sampleFile.OpenAsync(FileAccessMode.Read);
IInputStream inputStream = readStream.GetInputStreamAt(0);
DataReader dataReader = newDataReader(inputStream);
uint numBytesLoaded = await dataReader.LoadAsync((uint)readStream.Size);
string result = dataReader.ReadString(numBytesLoaded);


Copying a file
StorageFile sampleFile = await storageFolder.GetFileAsync(filename);
StorageFile copyFile = await sampleFile.CopyAsync(storageFolder, "sample - Copy.txt", NameCollisionOption.GenerateUniqueName);

Deleting a file

StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
await sampleFile.DeleteAsync();

你可能感兴趣的:(metro)