Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Winforms_Foreach_Text Files - Fatal编程技术网

将文本文件中的每一行放入数组C#

将文本文件中的每一行放入数组C#,c#,arrays,winforms,foreach,text-files,C#,Arrays,Winforms,Foreach,Text Files,我正在通过C#中的Windows窗体选择一个带有文件夹路径的文本文件,并收集每个路径的信息。此时,我可以导入文件并仅显示文本文件中的第二个路径,但没有关于文件夹的信息。以下是我的代码: private void btnFilePath_Click(object sender, EventArgs e) { //creating a stream and setting its value to null Stream myStream = null;

我正在通过C#中的Windows窗体选择一个带有文件夹路径的文本文件,并收集每个路径的信息。此时,我可以导入文件并仅显示文本文件中的第二个路径,但没有关于文件夹的信息。以下是我的代码:

private void btnFilePath_Click(object sender, EventArgs e)
    {
        //creating a stream and setting its value to null
        Stream myStream = null;

        //allowing the user select the file by searching for it
        OpenFileDialog open = new OpenFileDialog();
        open.InitialDirectory = "c:\\";
        open.Filter = "txt files (*.txt)|*.txt";
        open.FilterIndex = 2;
        open.RestoreDirectory = true;

        //if statement to print the contents of the file to the text box
        if (open.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = open.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        txtFilePath.Text = string.Format("{0}", open.FileName);

                        if (txtFilePath.Text != "")
                        {

                            lstFileContents.Text = System.IO.File.ReadAllText(txtFilePath.Text);

                            //counting the lines in the text file
                            using (var input = File.OpenText(txtFilePath.Text))
                            {
                                while (input.ReadLine() != null)
                                {
                                        //getting the info
                                        lstFileContents.Items.Add("" + pathway);
                                        pathway = input.ReadLine();
                                        getSize();
                                        getFiles();
                                        getFolders();
                                        getInfo();
                                    result++;
                                }

                                MessageBox.Show("The number of lines is: " + result, "");
                                lstFileContents.Items.Add(result);
                            }

                        }
                        else
                        {
                            //display a message box if there is no address
                            MessageBox.Show("Enter a valid address.", "Not a valid address.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read the file from disk. Original error: " + ex.Message);
            }
        }
    }
我认为应该使用foreach将每一行复制到一个变量,或者将每一行放入一个数组中,并循环通过它来收集信息

谁能告诉我哪一个最合适,这样我就可以去MSDN为自己学习了,因为我更喜欢学习它,而不是得到代码


谢谢

只需将所有行收集到数组中,我将使用

var lines = File.ReadAllLines(path);

我不确定你的问题是什么,因为你似乎已经回答了。如果您希望我们对其进行审查,您的问题将更适合于代码审查:

如果要使用MSDN,请查看以下内容:

剧透警报,我会这样做:

    string[] lines = null;
    try
    {
        lines = File.ReadAllLines(path);
    }
    catch(Exception ex)
    {
        // inform user or log depending on your usage scenario
    }

    if(lines != null)
    {
        // do something with lines
    }

如果你想有更多的参考,而不是答案本身,那么就把这些链接一个接一个地拿出来,所有这些链接都以不同的方式解释事情


希望它能帮助您更多地了解C#中的文件IO操作。

@Guruston感谢您的支持。这确实有助于改进我的答案。