开发者

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

How to开发者_如何转开发 read each individual inputted text on their respective text block? When i activate the reading codes, they read the 2 text in 1 text block.

Saving Codes:

private void OnSaveFile(string filePath)
    {

        StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(filePath, UriKind.Relative));

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            string directoryName = System.IO.Path.GetDirectoryName(filePath);
            if (!string.IsNullOrEmpty(directoryName) && !myIsolatedStorage.DirectoryExists(directoryName))
            {
                myIsolatedStorage.CreateDirectory(directoryName);
            }

            using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(fileStream))
                {


                    writer.WriteLine(text1);
                    writer.WriteLine(text2);


                }
            }
        }
    }

Reading Codes:

    private void OnReadSelected(string filePath)
        {

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(filePath))
                {
                    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Read))
                    {
                        using (StreamReader reader = new StreamReader(fileStream))
                        {
                            this.textBlocky1.Text = reader.ReadLine();
                            this.textBlocky2.Text = reader.ReadLine();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Files are not Found!");

                }
            }
        }


You are writing the text as a single line and then reading it as 2.

Instead of:

string someTextData = textFileName.Text + text1.Text;    
writer.WriteLine(someTextData);

you could do:

writer.WriteLine(textFileName.Text);   
writer.WriteLine(text1.Text);

Update
Here's a working version:

assuming the page contains:

<TextBlock Text="item 1" />
<TextBox Name="textItem1" />
<TextBlock Text="item 2" />
<TextBox Name="textItem2" />
<Button Content="write" Click="WriteClick" />
<Button Content="read" Click="ReadClick" />

The following in the code behind will do what you want.

private void WriteClick(object sender, RoutedEventArgs e)
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var fileStream = store.OpenFile("myfile.txt", FileMode.Create, FileAccess.Write))
        {
            using (var writer = new StreamWriter(fileStream))
            {
                writer.WriteLine(textItem1.Text);
                writer.WriteLine(textItem2.Text);
            }
        }
    }
}

private void ReadClick(object sender, RoutedEventArgs e)
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (store.FileExists("myfile.txt"))
        {
            using (var fileStream = store.OpenFile("myfile.txt", FileMode.Open, FileAccess.Read))
            {
                using (var reader = new StreamReader(fileStream))
                {
                    textItem1.Text = reader.ReadLine();
                    textItem2.Text = reader.ReadLine();
                }
            }
        }
    }
}


While writing u insert a newline character \n in between the text which u want to be read as separate lines. The Readline() function reads till it encounters a newline character i.e \n. So whatever u want to be read as a separate line, insert a \n after that line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜