2011-07-29 21 views
0

我想從一個文本框到internalStorage保存文本,並從那裏加載它...Phone7的,另一個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。

如果有人給我一些技巧來修復代碼,那真的太棒了。

在此先感謝..

+0

你是從模擬器加載文件?因爲孤立的文件存儲從一個空白的石板開始 –

+0

耶,我知道但是謝謝。我首先創建了一些測試文件... – infinitesimalLeanne

+0

例外情況是:IsolatedStorageFileStream不允許操作。 – infinitesimalLeanne

回答

1

我這是怎麼打開ISF流

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

我會嘗試它並報告:) thx – infinitesimalLeanne

+0

不,它不會爲我工作,仍然:IsolatedStorageFileStream不允許操作 – infinitesimalLeanne

+0

什麼是文件名? –

1

供參考:不要嘗試沒有電話來測試,如果你想與獨立存儲工作。

這次終於爲我工作:

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

也許你看到我的使用(..),並把在「空」有點檢查喪生。我認爲主要原因是沒有電話來測試代碼。

非常感謝你確實:-)))