How to transform array into textfile?

末鹿安然 提交于 2020-01-06 07:09:27

问题


I am currently creating a windows phone 7 application. How do I transform all the values of an array to textfile so I can print out all the values stored in that particular array inside another page listbox, and then store inside an isolated storage. Thanks! :(

I tried this method but it doesn't work. For 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.");

        }

回答1:


You must write away the serialized items/data of the listbox, not the listbox itself.

When reading them again, you must deserialize them.The BinaryFormatter example on MSDN has code samples showing you how to write away an object to a (file)stream and how to recover the object again from that same stream.




回答2:


If you want to display contents of your file (say, lines) in listbox, read entire file content at once and split the lines:

string fileText = readFile.ReadToEnd();
string[] lines = fileText.Split(Enviroment.NewLine.ToCharArray(), 
                                StringSplitOptions.RemoveEmptyEntries);
listNumberListBox.Items.AddRange(lines);

Other way around is similar, fetch items from your listbox, by joining them with new line, and dump at once to file:

string fileContents = string.Join(Environment.NewLine, 
                                  retrieveDrinksListBox.Items.Cast<string>());
writeFile.WriteLine(fileContents);


来源:https://stackoverflow.com/questions/5740729/how-to-transform-array-into-textfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!