How to read individual lines of a text from the isolated storage?

北战南征 提交于 2019-12-14 03:34:47

问题


(EDITED)

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!");
                }
            }
        }

CreateTextPage:

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

        }

ReadFilePage:

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

    }

回答1:


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'.

Try changing

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

to

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

Also, change

using (var reader = new StreamReader(fileStream))

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

to

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

to get your code to compile.

UPDATE:

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

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

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'?



来源:https://stackoverflow.com/questions/6639588/how-to-read-individual-lines-of-a-text-from-the-isolated-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!