UWP 文件读写API
1. 读取普通Pictures下面文件:
public async Task ShowFileContent(string fileName) { string msgString = ""; IStorageFolder applicationFolder = KnownFolders.PicturesLibrary; IStorageFile file = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists); IRandomAccessStream accessStream = await file.OpenReadAsync(); using (StreamReader streamReader = new StreamReader(accessStream.AsStreamForRead((int)accessStream.Size))) { string content = streamReader.ReadToEnd(); if ("" == content) msgString = "null"; else msgString = content; } await new MessageDialog(msgString).ShowAsync(); }
2. 读二进制文件,转16进制
IStorageFolder applicationFolder = KnownFolders.PicturesLibrary; IStorageFile file = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists); IRandomAccessStream accessStream = await file.OpenReadAsync(); using (BinaryReader binReader = new BinaryReader(accessStream.AsStreamForRead((int)accessStream.Size))) { byte[] buffer = new byte[(int)accessStream.Size]; binReader.Read(buffer, 0, buffer.Length); string fullString = ""; for(int i=3;i<buffer.Length;i++) { fullString += String.Format("{0:X2}", buffer[i]); } string WifiMAC = fullString.Substring(6,12); }