2013-12-09 82 views
1

我創建了一個獨立存儲,它只存儲一個值並顯示一個值。 "aaaaaaaa,aaaaaaaa"
我怎樣才能讓這個它存儲
1)"aaaaaaaa,aaaaaaaaa"
2)"bbbbbbbb,bbbbbbbbb"
3)"cccccccccc,cccccccccc"將多個值存儲到獨立存儲中

下面表明,只存儲一個值碼:

//get the storage for your app 
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); 
//define a StreamWriter 
StreamWriter writeFile; 

if (!store.DirectoryExists("SaveFolder")) 
{ 
    //Create a directory folder 
    store.CreateDirectory("SaveFolder"); 
    //Create a new file and use a StreamWriter to the store a new file in 
    //the directory we just created 
    writeFile = new StreamWriter(new IsolatedStorageFileStream(
     "SaveFolder\\SavedFile.txt", 
     FileMode.CreateNew, 
     store)); 
} 
else 
{ 
    //Create a new file and use a StreamWriter to the store a new file in 
    //the directory we just created 
    writeFile = new StreamWriter(new IsolatedStorageFileStream(
     "SaveFolder\\SavedFile.txt", 
     FileMode.Append, 
     store)); 
} 

StringWriter str = new StringWriter(); 
str.Write(textBox1.Text); 
str.Write(","); 
str.Write(textBox2.Text); 

writeFile.WriteLine(str.ToString()); 
writeFile.Close(); 

textBox1.Text = string.Empty; 
textBox2.Text = string.Empty; 

    StringWriter str = new StringWriter(); 
str.Write(textBox1.Text); 
str.Write(","); 
str.Write(textBox2.Text); 


writeFile.WriteLine(str.ToString()); 
writeFile.Close(); 

textBox1.Text = string.Empty; 
textBox2.Text = string.Empty;  
+1

目前還不清楚你正在嘗試做什麼 - 創建一個多行文件或創建多個文件。 –

+0

帶有多行的單個文件,謝謝修正。 – NoobieNeedHelp

+0

對,多次調用'WriteLine',每行一次... –

回答

2

您可以使用WriteLine幾次如下

writeFile.WriteLine(string.Format("{0},{1}", "aaaaa","aaaaa")); 
writeFile.WriteLine(string.Format("{0},{1}", "bbbbb","bbbbb")); 
writeFile.WriteLine(string.Format("{0},{1}", "ccccc","ccccc")); 
1

我可以確認Damith的答案,並且應該標記爲這樣。

如果你想知道關於讀線回:

IsolatedStorageFileStream stream = new IsolatedStorageFileStream("SaveFolder", FileMode.Open, store); 

StreamReader reader = new StreamReader(stream); 

string rdr = reader.ReadLine(); 

RDR將代表第一行的字符串,然後你可以再打電話ReadLine()獲得下一行,等等。第一個電話到ReadLine()將返回您寫入商店的第一行。

希望這對你或者在這方面工作的其他人有用。

+0

是的,這是有幫助的,我可以知道ReadLine而不是ReadToEnd,我怎樣才能得到一個讀線循環讀線再檢索它。 – NoobieNeedHelp