Windows phone 7 Phone7,另一个隔离存储文件问题
我想将文本从文本框保存到内部存储并从那里加载 保存部分工作正常。但是加载不起作用,我已经试过很多教程了Windows phone 7 Phone7,另一个隔离存储文件问题,windows-phone-7,text,isolatedstorage,Windows Phone 7,Text,Isolatedstorage,我想将文本从文本框保存到内部存储并从那里加载 保存部分工作正常。但是加载不起作用,我已经试过很多教程了 private void button2_Click(object sender, RoutedEventArgs e) { //get selected FileName from listBox string selItem = listBox1.SelectedItem.ToString(); IsolatedStorageFile
private void button2_Click(object sender, RoutedEventArgs e)
{
//get selected FileName from listBox
string selItem = listBox1.SelectedItem.ToString();
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (selItem != null)
{
IsolatedStorageFileStream fileStream = store.OpenFile(selItem, FileMode.Open, FileAccess.Read);
using (StreamReader sr = new StreamReader(fileStream))
{
String line = "";
//Debug.WriteLine("ReadLine");
if ((line = sr.ReadLine()) != null)
{
//Debug.WriteLine("ReadLineText");
textBox1.Text = line;
}
sr.Close();
}
fileStream.Close();
}
}
而不是:
if ((line = sr.ReadLine()) != null)
{
//Debug.WriteLine("ReadLineText");
textBox1.Text = line;
我已经尝试了很多可能性,比如:textBox1.Text=sr.ReadLine();等等
代码的奇怪之处在于:例如,如果我输入:
IsolatedStorageFileStream fileStream = store.OpenFile("text0.txt", FileMode.Open, FileAccess.Read);
它适用于单个文件text0.txt
如果有人能给我一些修改代码的建议,那就太好了
提前感谢。这就是我打开ISF流的方式
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, isf); // loads from isolated storage
仅供参考:如果您想使用隔离存储,请不要尝试在没有手机的情况下进行测试 这终于对我起作用了:
private void button2_Click(object sender, RoutedEventArgs e)
{
//get fileName
string filename = listBox1.SelectedItem.ToString();
try
{
IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, store); // loads from isolated storage
//Debug.WriteLine(stream.CanRead);
StreamReader sr = new StreamReader(stream);
String lines = sr.ReadToEnd().ToString();
if (lines != null)
{
textBox1.Text = lines;
}
stream.Close();
sr.Close();
}
catch (Exception)
{
throw;
}
}
}
也许你看到了,我用(…)删除了,并在“Null”上做了一点检查。我认为主要原因是没有电话测试代码
非常感谢:-)是否从模拟器加载文件?因为独立文件存储每天都从一张白纸开始,我知道,但谢谢。我首先创建了几个测试文件…例外情况是:不允许对IsolatedStorageFileStream执行操作。失败时selItem的值是多少?不,它对我不起作用,仍然是:不允许对IsolatedStorageFileStream执行操作
string filename=listBox1.SelectedItem.ToString();IsolatedStorageFile isf=IsolatedStorageFile.GetUserStoreForApplication();IsolatedStorageFileStream=新的IsolatedStorageFileStream(文件名,FileMode.OpenOrCreate,isf);//来自独立存储StreamReader sr的负载=新StreamReader(流);textBox1.Text=sr.ReadLine()代码>首先对文件名进行硬编码,以确保首先解决了这个问题我已经解决了。如果我将它直接硬编码到IsolatedStorageFileStream=[…]的定义中,就像“filename”一样,它可以正常工作。但不是动态的:string filename=listBox1.SelectedItem.ToString()崩溃..通过ListBox.SelectedItem调用获得的文件名(字符串)是什么?