问题描述
限时送ChatGPT账号..我的目标是将文本框"输入保存到文本文件中,然后能够将保存的文本从同一文本文件加载回文本框.
My objective is to save "textbox" input to a text file and then being able to load that saved text from the same text file back to a textbox.
我认为我的错误之一是在控制台上阅读.
I think that one of my mistakes is reading on Console.
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.
Use Windows.Storage.StorageFile in both Windows Phone Runtime and Windows Phone Silverlight apps.
Working与数据和文件部分.特别是请参阅快速入门:本地应用数据或快速入门:漫游应用数据和快速入门:读取和写入文件
There are examples in the MSDN documentation in the Working with data and files section. In particular see Quickstart: Local app data or Quickstart: Roaming app data and Quickstart: Reading and writing files
最后一个 Quickstart 演示了直接在文件中读取和写入短文本片段的演示:
This last Quickstart has demos directly on reading and writing short text snippets to a file:
// 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 Phone 8.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论