我目前正在创建一个Windows Phone 7应用程序。如何将数组的所有值转换为textfile,以便打印出存储在另一个页面列表框中的特定数组中的所有值,然后存储在隔离的存储中。 谢谢! :(
我尝试过这种方法,但它不起作用。 适用于ViewList.xaml.cs
private void addListBtn_Click(object sender, RoutedEventArgs e)
{
//Obtain the virtual store for application
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new folder and call it "ImageFolder"
myStore.CreateDirectory("ListFolder");
//Create a new file and assign a StreamWriter to the store and this new file (myFile.txt)
//Also take the text contents from the txtWrite control and write it to myFile.txt
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.OpenOrCreate, myStore));
writeFile.WriteLine(retrieveDrinksListBox);
writeFile.Close();
}
FavouriteList.xaml.cs
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
//Obtain a virtual store for application
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
//This code will open and read the contents of retrieveDrinksListBox
//Add exception in case the user attempts to click “Read button first.
StreamReader readFile = null;
try
{
readFile = new StreamReader(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.Open, myStore));
string fileText = readFile.ReadLine();
if (fileText != "")
{
listNumberListBox.Items.Add("List 1");
}
//The control txtRead will display the text entered in the file
listNumberListBox.Items.Add(fileText);
readFile.Close();
}
catch
{
MessageBox.Show("Need to create directory and the file first.");
}
答案 0 :(得分:0)
您必须写下列表框的序列化项目/数据,而不是列表框本身。
再次阅读它们时,必须反序列化它们.MSDN上的BinaryFormatter示例有代码示例,向您展示如何将对象写入(文件)流以及如何从同一个流中再次恢复对象。
答案 1 :(得分:0)
如果要在列表框中显示文件的内容(例如,行),请立即阅读整个文件内容并split行:
string fileText = readFile.ReadToEnd();
string[] lines = fileText.Split(Enviroment.NewLine.ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);
listNumberListBox.Items.AddRange(lines);
其他方式类似,从列表框中获取项目,joining使用新行,并立即转储到文件:
string fileContents = string.Join(Environment.NewLine,
retrieveDrinksListBox.Items.Cast<string>());
writeFile.WriteLine(fileContents);