如何将数组转换成文本文件? [英] How to transform array into textfile?

查看:148
本文介绍了如何将数组转换成文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个Windows Phone 7应用程序。我如何变换数组的所有值设置为文本文件,所以我可以打印出存储在另一个页面列表框中特定的阵列中的所有值,然后存储在独立存储中。
谢谢! (

这个方法我试过,但它不工作。
对于ViewList.xaml.cs

 私人无效addListBtn_Click(对象发件人,RoutedEventArgs E)
    {
        //获取应用程序的虚拟商店
        IsolatedStorageFile的MyStore = IsolatedStorageFile.GetUserStoreForApplication();        //创建一个新的文件夹并将其命名为ImageFolder
        myStore.CreateDirectory(ListFolder);
        //创建一个新的文件并分配的StreamWriter到店里和这个新的文件(MYFILE.TXT)
        //同样来自txtWrite控制采取文本内容并把它写入MYFILE.TXT        StreamWriter的WriteFile的=新的StreamWriter(新IsolatedStorageFileStream(ListFolder \\\\ MYFILE.TXT,FileMode.OpenOrCreate,的MyStore));
        writeFile.WriteLine(retrieveDrinksListBox);
        writeFile.Close();
     }

FavouriteList.xaml.cs

 私人无效PhoneApplicationPage_Loaded(对象发件人,RoutedEventArgs E)
    {
        //获取应用程序的虚拟商店
        IsolatedStorageFile的MyStore = IsolatedStorageFile.GetUserStoreForApplication();        //这个code将打开并阅读retrieveDrinksListBox的内容
        //添加的情况下,用户尝试单击首先阅读按钮例外。        StreamReader的READFILE = NULL;        尝试
        {
            READFILE =新的StreamReader(新IsolatedStorageFileStream(ListFolder \\\\ MYFILE.TXT,FileMode.Open,的MyStore));
            字符串FILETEXT = readFile.ReadLine();            如果(FILETEXT!=)
            {                listNumberListBox.Items.Add(列表1);
            }            //控制txtRead将显示在文件中输入的文本
            listNumberListBox.Items.Add(FILETEXT);
            readFile.Close();        }        抓住
        {            的MessageBox.show(需要先创建目录和文件。);        }


解决方案

如果你想显示的列表框中文件的内容(例如,线),一次读取整个文件的内容和的分裂行:

 字符串FILETEXT = readFile.ReadToEnd();
字符串[] =行fileText.Split(Enviroment.NewLine.ToCharArray()
                                StringSplitOptions.RemoveEmptyEntries);
listNumberListBox.Items.AddRange(线);

其他方式是类似的,由加盟它们与新的生产线,并转储在一次文件:

 字符串fileContents =的string.join(Environment.NewLine,
                                  retrieveDrinksListBox.Items.Cast<串GT;());
writeFile.WriteLine(fileContents);

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.");

        }

解决方案

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

这篇关于如何将数组转换成文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆