2011-01-12 216 views
0

我已經在「汽車」文件中創建了IO 中的一些文件,我想把一些其他參考如模型,顏色等...... 所以我的問題是:是有可能有在IO 多襯裏的文件,如果是我怎樣才能在StreamReader的 讓他們//我想在一個文件中存儲的許多參數,並通過StreamReaderIsolatedStorage @Windows Phone 7

保護覆蓋無效找回來OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {

  //reception des parametres de la listbox 
      base.OnNavigatedTo(e); 
      string parameter = this.NavigationContext.QueryString["parameter"]; 
      this.tbTitre.Text = parameter; 
      try 
      { 
       //Create a new StreamReader 
       StreamReader editionDevisReader = null; 
       IsolatedStorageFile probyOrange = IsolatedStorageFile.GetUserStoreForApplication(); 
       //Read the file from the specified location. 
       editionDevisReader = new StreamReader(new IsolatedStorageFileStream("devis\\"+parameter+".txt", FileMode.Open, probyOrange)); 
       //Read the contents of the file . 
       string textFile = editionDevisReader.ReadLine(); 

       //Write the contents of the file to the TextBlock on the page. 
       tbTitre.Text = textFile; 

       while (editionDevisReader != null) 
       { 
        RowDefinition rowdefinition = new RowDefinition(); 

        TextBlock textblock = new TextBlock(); 
        textblock.HorizontalAlignment = new System.Drawing.Size(48, 20); 

       } 
       editionDevisReader.Close(); 
      } 
      catch 
      { 
       //If the file hasn't been created yet. 
       tbTitre.Text = "veuillez d abord creer le fichier"; 
      } 

THX很多都

回答

1

是的,你可以在一個文件中保存任何東西(最高點):

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    using (var isfs = new IsolatedStorageFileStream("myfile.txt", FileMode.OpenOrCreate, store)) 
    { 
     using (var sw = new StreamWriter(isfs)) 
     { 
      sw.Write("anything really. Here it's just a string but could be a serialized object, etc."); 
      sw.Close(); 
     } 
    } 
} 

然後,您可以讀取該文件:

var result = string.Empty; 

try 
{ 
    using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     if (!store.FileExists("myfile.txt")) 
     { 
      return result; 
     } 

     using (var isfs = new IsolatedStorageFileStream("myfile.txt", FileMode.Open, store)) 
     { 
      using (var sr = new StreamReader(isfs)) 
      { 
       string lineOfData; 

       while ((lineOfData = sr.ReadLine()) != null) 
       { 
        result += lineOfData; 
       } 
      } 
     } 
    } 
} 
catch (IsolatedStorageException) 
{ 
    result = string.Empty; // may have partial data/file before error 
} 

return result; 
+0

我明白了,我想知道如何在streamreader中讀取它們。如果我創建一個文件「car.txt」,我想在文件中加入顏色,模型等......所以我怎麼讀它們? thx很多爲你的幫助 – user569574 2011-01-12 13:53:38

+0

@ user569574我已經添加了一個閱讀文件的例子 – 2011-01-13 09:15:05

0

你可以使用

StreamReader.Writeline 

StreamRead.ReadLine 

寫入和讀取由換行符分隔的文本塊。