如何从独立存储中读取文本的单行? [英] How to read individual lines of a text from the isolated storage?

查看:35
本文介绍了如何从独立存储中读取文本的单行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(已编辑)

我的代码无法读取文本文件.显然,当我想阅读文件时,他们给了我一个找不到文件!"if else循环中的消息框,说明if循环中的代码不起作用.

My codes doesn't read the text file. Apparent;y, when I want to read the file, they gave me a "File Not Found!" message box which it is in the if else loop, indicating that the code in the if loop doesn't work.

        private void OnSaveFile()
        {
            if (!string.IsNullOrEmpty(this.FileName))
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(FileName))
                        store.DeleteFile(FileName);

                    using (var fileStream = store.OpenFile(FileName, FileMode.Create, FileAccess.Write))
                    {
                        using (var writer = new StreamWriter(fileStream))
                        {

                            writer.WriteLine(FileName);
                            writer.WriteLine(FileText1);
                            writer.WriteLine(FileText2);


                        }
                    }
                }
            }
        }

        private void OnReadSelected()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(FileName))
                {
                    using (var fileStream = store.OpenFile(FileName, FileMode.Open, FileAccess.Read))
                    {
                        using (var reader = new StreamReader(fileStream))
                        {
                            FileName = reader.ReadLine();
                            FileText1 = reader.ReadLine();
                            FileText2 = reader.ReadLine();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("File not found!");
                }
            }
        }

创建文本页面:

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            AddFileModel model = this.LayoutRoot.DataContext as AddFileModel;
            model.SaveFile.Execute(null);
            model.FileName = string.Empty;

            model.FileText1 = string.Empty;
            model.FileText2 = string.Empty;


            MessageBox.Show("File saved successfully");

            NavigationService.Navigate(new Uri("/CompleteQuestionPage.xaml", UriKind.Relative));

        }

读取文件页:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        AddFileModel model = this.LayoutRoot.DataContext as AddFileModel;
        model.ReadSelectedFiles.Execute(null);

    }

推荐答案

在您的 OnSaveFile 方法中,您似乎将数据保存到名为myfile.txt"的文件中.在 OnReadSelected 中,您使用文件名"属性打开文件.文件名"设置为什么?因为如果它未设置为myfile.txt",那么这可能就是您看到找不到文件"的原因.

In your OnSaveFile method, it looks like you're saving the data to a file called "myfile.txt". In OnReadSelected you're opening a file using the 'Filename' property. What is 'Filename' set to? Because if it is not set to "myfile.txt", then that's probably why you're seeing 'File not found'.

尝试改变

using (var fileStream = store.OpenFile("myfile.txt", FileMode.Create, FileAccess.Write))

using (var fileStream = store.OpenFile(Filename, FileMode.Create, FileAccess.Write))

另外,改变

using (var reader = new StreamReader(fileStream))

FileName = reader.ReadLine();
FileText1 = reader.ReadLine();
FileText2 = reader.ReadLine();

using (var reader = new StreamReader(fileStream))
{  
   FileName = reader.ReadLine();
   FileText1 = reader.ReadLine();
   FileText2 = reader.ReadLine();
}

让你的代码编译.

更新:

好的,现在在您的按钮单击处理程序中,您正在执行以下操作:

Okay, right now in your button click handler you're doing this:

model.SaveFile.Execute(null);
model.FileName = string.Empty;

您将文件名"的值设置为空字符串;稍后,您将再次调用 OnReadSelected,它使用文件名"中的值.由于它已设置为空字符串,因此我认为这就是您看到找不到文件"的原因.如果删除该行会发生什么

You're setting the value of 'Filename' to be an empty string; later on, you're called OnReadSelected again, which uses the value in 'Filename'. Since it's been set to an empty string, I think that's why you're seeing 'File Not Found'. What happens if you remove the line

model.FileName = string.Empty;

?您是否仍然收到找不到文件"的提示?

? Do you still get 'File Not Found'?

这篇关于如何从独立存储中读取文本的单行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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