gpt4 book ai didi

c# - 读写文本文件 Windows Phone 8.1

转载 作者:太空狗 更新时间:2023-10-30 00:50:38 24 4
gpt4 key购买 nike

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

我认为我的错误之一是在控制台上阅读。

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。

Working with data and files 中的 MSDN 文档中有示例部分。特别参见 Quickstart: Local app dataQuickstart: Roaming app dataQuickstart: Reading and writing files

最后一个快速入门有直接在文件中读取和写入短文本片段的演示:

// 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);

关于c# - 读写文本文件 Windows Phone 8.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30669491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com