如何从孤立的存储器中读取文本的各个行
本文关键字:读取 取文本 存储器 | 更新日期: 2023-09-27 18:01:26
如何在各自的文本块上读取每个单独输入的文本?当我激活阅读代码时,它们读取1个文本块中的2个文本。
保存代码:
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);
}
}
}
}
阅读代码:
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!");
}
}
}
您将文本写成一行,然后将其读取为2。
代替:
string someTextData = textFileName.Text + text1.Text;
writer.WriteLine(someTextData);
你可以这样做:
writer.WriteLine(textFileName.Text);
writer.WriteLine(text1.Text);
下面是一个工作版本:
假设页面包含:
<TextBlock Text="item 1" />
<TextBox Name="textItem1" />
<TextBlock Text="item 2" />
<TextBox Name="textItem2" />
<Button Content="write" Click="WriteClick" />
<Button Content="read" Click="ReadClick" />
后面的代码可以满足你的要求。
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();
}
}
}
}
}
在写u的时候,在你想作为单独的行读取的文本之间插入一个换行字符'n。Readline()函数读取,直到遇到换行字符,即'n。因此,无论您想将其作为单独的行读取,请在该行之后插入'n