Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数组转换为文本文件?_C#_Arrays_Windows Phone 7 - Fatal编程技术网

C# 如何将数组转换为文本文件?

C# 如何将数组转换为文本文件?,c#,arrays,windows-phone-7,C#,Arrays,Windows Phone 7,我目前正在创建一个windows phone 7应用程序。如何将数组的所有值转换为textfile,以便在另一个页面列表框中打印出存储在该特定数组中的所有值,然后存储在独立的存储中。 谢谢( 我试过这个方法,但不起作用。 用于ViewList.xaml.cs private void addListBtn_Click(object sender, RoutedEventArgs e) { //Obtain the virtual store for applica

我目前正在创建一个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();
     }
    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.");

        }
收藏夹列表.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();
     }
    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.");

        }

必须删除列表框的序列化项/数据,而不是列表框本身


再次读取对象时,必须对其进行反序列化。MSDN上的示例提供了代码示例,说明如何将对象写入(文件)流,以及如何从该流中再次恢复该对象。

如果要在列表框中显示文件内容(例如,行),请立即读取整个文件内容,并显示以下行:

string fileText = readFile.ReadToEnd();
string[] lines = fileText.Split(Enviroment.NewLine.ToCharArray(), 
                                StringSplitOptions.RemoveEmptyEntries);
listNumberListBox.Items.AddRange(lines);
另一种方法与此类似,从列表框中提取项目,并使用新行将其转储到文件:

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