读取和写入文本文件 Windows Phone 8.1

编程入门 行业动态 更新时间:2024-10-27 15:30:06
本文介绍了读取和写入文本文件 Windows Phone 8.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-25 13:22:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本文件   Windows   Phone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!