metro File IO
程序员文章站
2022-06-04 11:41:57
...
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();
推荐阅读
-
node.js + socket.io 实现点对点随机匹配聊天
-
JAVA.io读写文件方式汇总
-
Linux系统下的CPU、内存、IO、网络的压力测试
-
python day 15: IO多路复用,socketserver源码培析,
-
Windows8系统Metro界面下的多多五笔输入法使用介绍
-
Linux 如何避免IO hang实现方法
-
php中file_get_content 和curl以及fopen 效率分析
-
Java IO编程——转换流
-
为PHP安装imagick时出现Cannot locate header file MagickWand.h错误的解决方法
-
新加坡衍生品交易新秀BEX.io亮相 六大理由成为合约首选