如何正确地使用Windows读写文件.Windows Phone 8的存储空间
本文关键字:Windows Phone 存储空间 文件 读写 正确地 | 更新日期: 2023-09-27 18:05:40
我需要的只是简单地写入文件,并从Windows Phone 8中使用Windows读取文件。存储api。使用旧的IsolatedStorage方法这相对容易,但使用新的WinRT API则要困难得多。
我一直在试着写它,但似乎有所有这些奇怪的东西,如IBuffer。等。使用完整版的WinRT,使用Windows.Storage.FileIO非常容易,它的存在似乎是为了防止像我这样的开发人员发疯。然而,它没有在Phone版本中实现。另外,我观看了Channel9的一个视频,其中展示了一些代码示例,但是他们有一个错误,他们使用标记为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<string> 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工具包在Windows 8上可以工作的。
using System;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
namespace WinRTXamlToolkit.IO.Extensions
{
/// <summary>
/// Extensions for simple writing and reading of strings to/from files.
/// </summary>
/// <remarks>
/// Note that these were created before FileIO class existed in WinRT, but they still serve a purpose.
/// </remarks>
public static class StringIOExtensions
{
/// <summary>
/// Reads a string from a text file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="folder">The folder.</param>
/// <returns></returns>
public static async Task<string> 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;
}
}
}
}
/// <summary>
/// Writes a string to a text file.
/// </summary>
/// <param name="text">The text to write.</param>
/// <param name="fileName">Name of the file.</param>
/// <param name="folder">The folder.</param>
/// <param name="options">
/// 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.
/// </param>
/// <returns></returns>
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();
}
}
}
}
}