开发者

Phone7, another IsolatedStorageFile problem

I want to save text from a textbox to the internalStorage and load it from there...

The saving-part works fine. But the loading won't work I tried many tutorials already.

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开发者_JAVA技巧 = "";
                //Debug.WriteLine("ReadLine");
                if ((line = sr.ReadLine()) != null)
                {
                    //Debug.WriteLine("ReadLineText");
                    textBox1.Text = line;
                }
                sr.Close();
            }
            fileStream.Close();
        }
    }

Instead of:

if ((line = sr.ReadLine()) != null)
            {
                //Debug.WriteLine("ReadLineText");
                textBox1.Text = line;

I've tried many possibilities like: textBox1.Text = sr.ReadLine(); and so on..

The curious thing about he code is: If I enter for example:

IsolatedStorageFileStream fileStream = store.OpenFile("text0.txt", FileMode.Open, FileAccess.Read);

It works fine for the single file text0.txt.

Would be really really great if someone give me some tips to fix the code.

Thanks in advance..


this is how I open an ISF Stream

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, isf);  // loads from isolated storage


FYI: don't try to test without phone if you want to work with the isolated storage.

this finally works for me:

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

Maybe you see I killed the using(..) and put in a little check on "Null". I think the main cause was that there was no phone present to test the code.

Thank you very much indeed :-)))

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜