在Windows Phone 8.1的文本文件上读取和写入

我的目标是将“文本框”输入保存到文本文件,然后能够将保存的文本从同一文本文件加载回文本框。 有帮助吗?

我认为我的一个错误是在控制台上阅读……

namespace aaa { public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { string fileName = "test.txt"; FileStream fs = null; fs = new FileStream(fileName, FileMode.CreateNew); StreamWriter writer = new StreamWriter(fs); writer.Write(textBox1.Text); } private void btnWrite_Click(object sender, RoutedEventArgs e) { } private void btnRead_Click(object sender, RoutedEventArgs e) { public static void Main() { using (StreamReader sr = new StreamReader("test.txt")) { string line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } } } } 

在Windows Phone运行时和Windows Phone Silverlight应用程序中使用Windows.Storage.StorageFile。

“ 使用数据和文件”部分的MSDN文档中有一些示例。 特别参见快速入门:本地应用数据或快速入门:漫游应用数据和快速入门:读取和写入文件

最后一个快速入门直接在阅读和编写短文本片段到文件时进行演示:

 // Create sample file; replace if exists. StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder; StorageFile sampleFile = await folder.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting); await Windows.Storage.FileIO.WriteTextAsync(sampleFile, textBox1.text); 

在Windows手机中你没有这样的文件系统直接访问权限。

您可以使用隔离文件存储来读写文件。

在这里您可以找到这些function的广泛概述:

https://msdn.microsoft.com/library/system.io.isolatedstorage.isolatedstoragefile

(注意:这是针对Windows Phone 7,对于Windows Phone 8.1,请参阅其他答案)