如何在Windows Phone 8上使用Windows.Storage正确读取和写入文件

我只需要使用Windows.Storage API简单地写入文件并从Windows Phone 8中的文件读取。 使用旧的IsolatedStorage方法相对容易,但使用新的WinRT APIcertificate它更加困难。

我一直试图写它,但似乎有所有这些奇怪的东西,如IBuffer。 等等。 使用WinRT的完整版本,使用Windows.Storage.FileIO非常容易,它似乎存在,以防止像我这样的开发人员疯狂。 但是,它没有在Phone版本中实现。 此外,我观看了一个Channel9video,其中显示了一些代码示例,但他们犯了一个错误,因为他们使用标记为Security Critical的方法获得常规流。 显然不允许获得常规流。

那么,有人可以为我提供一个简洁而正确的片段,介绍如何将文件读入字符串以及如何将字符串写入文件,并使用正确的使用和处理技术吗?

这是一个简单的例子:

 public async Task WriteDataToFileAsync(string fileName, string content) { byte[] data = Encoding.Unicode.GetBytes(content); var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (var s = await file.OpenStreamForWriteAsync()) { await s.WriteAsync(data, 0, data.Length); } } public async Task ReadFileContentsAsync(string fileName) { var folder = ApplicationData.Current.LocalFolder; try { var file = await folder.OpenStreamForReadAsync(fileName); using (var streamReader = new StreamReader(file)) { return streamReader.ReadToEnd(); } } catch (Exception) { return string.Empty; } } 

像这样使用它们:

 await this.WriteDataToFileAsync("afile.txt", "some text to save in a file"); var contents = await this.ReadFileContentsAsync("afile.txt"); 

我没有尝试使用Windows Phone 8,但这是WinRT XAML Toolkit可用于Windows 8的function。

 using System; using System.Threading.Tasks; using Windows.Storage; using Windows.Storage.Streams; namespace WinRTXamlToolkit.IO.Extensions { ///  /// Extensions for simple writing and reading of strings to/from files. ///  ///  /// Note that these were created before FileIO class existed in WinRT, but they still serve a purpose. ///  public static class StringIOExtensions { ///  /// Reads a string from a text file. ///  /// Name of the file. /// The folder. ///  public static async Task ReadFromFile( string fileName, StorageFolder folder = null) { folder = folder ?? ApplicationData.Current.LocalFolder; var file = await folder.GetFileAsync(fileName); using (var fs = await file.OpenAsync(FileAccessMode.Read)) { using (var inStream = fs.GetInputStreamAt(0)) { using (var reader = new DataReader(inStream)) { await reader.LoadAsync((uint)fs.Size); string data = reader.ReadString((uint)fs.Size); reader.DetachStream(); return data; } } } } ///  /// Writes a string to a text file. ///  /// The text to write. /// Name of the file. /// The folder. ///  /// The enum value that determines how responds if the fileName is the same /// as the name of an existing file in the current folder. Defaults to ReplaceExisting. ///  ///  public static async Task WriteToFile( this string text, string fileName, StorageFolder folder = null, CreationCollisionOption options = CreationCollisionOption.ReplaceExisting) { folder = folder ?? ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync( fileName, options); using (var fs = await file.OpenAsync(FileAccessMode.ReadWrite)) { using (var outStream = fs.GetOutputStreamAt(0)) { using (var dataWriter = new DataWriter(outStream)) { if (text != null) dataWriter.WriteString(text); await dataWriter.StoreAsync(); dataWriter.DetachStream(); } await outStream.FlushAsync(); } } } } }