Windows Phone应用程序-文件存储 [英] Windows Phone Apps - File Storage

查看:84
本文介绍了Windows Phone应用程序-文件存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件存储应用程序.

I have a file storage app.

这会将名称写到名为"names.txt"的文件中.

That writes a name to a file named "names.txt".

我的问题是,当我键入名称时,我单击一个按钮来存储名称.它可以正常工作,当我单击读取文件"时,便可以检索该名称.按钮.但是,当我将另一个名称写入文件时.当我单击读取文件"时,按钮.只有第二 显示的是名称,而不是名字和名字.

My problem is, that when ever i type in a name , and i click a button to store the name . it works I am able to retrieve that name when i click "read file" button. But when i write ANOTHER name to the file. When i click the "read file" button. Only the second name is shown, not the first AND second names.

写入文件的代码:

这在写入文件"被执行时执行.按钮.

this executes when "Write File" button is clicked.

 IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();


            using (var isoFileStream = new IsolatedStorageFileStream("names.txt", FileMode.OpenOrCreate, myStore))
            {
                //Write the data
                using (var isoFileWriter = new StreamWriter(isoFileStream))
                {
                    isoFileWriter.NewLine = "\n";
                    isoFileWriter.WriteLine(name);
                    
                    isoFileWriter.Close();
                }
            }

名称";是预定义的字符串.

"name" is a pre-defined String.

有人知道为什么它只读取存储的最新名称,而不读取它吗?

Does anyone know why it only reads the lastest name stored and none before it?

推荐答案

            IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();

            //If the file exists append in it
if (myStore.FileExists("myFile.txt")) { using (var isoFileStream = new IsolatedStorageFileStream("names.txt", FileMode.Append, myStore)) { //Write the data using (var isoFileWriter = new StreamWriter(isoFileStream)) { isoFileWriter.NewLine = "\n"; isoFileWriter.WriteLine(name); isoFileWriter.Close(); } } } else { using (var isoFileStream = new IsolatedStorageFileStream("names.txt", FileMode.Create, myStore)) { //Write the data using (var isoFileWriter = new StreamWriter(isoFileStream)) { isoFileWriter.NewLine = "\n"; isoFileWriter.WriteLine(name); isoFileWriter.Close(); } } }



这篇关于Windows Phone应用程序-文件存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆