将每行从文本文件放入数组C#

Mar*_*ark 2 c# arrays foreach text-files winforms

我正在通过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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在想使用foreach将每一行复制到一个变量,或者将每一行放入一个数组中并循环遍历它以收集信息.

任何人都可以告诉我哪个是最合适的,所以我可以去MSDN并为自己学习,因为,我更愿意学习它而不是给出代码.

谢谢!

mar*_*mnl 7

我不确定你的问题是什么,因为你似乎已经回答了它.如果您希望我们对其进行审核,您的问题将更适合代码审核:https://codereview.stackexchange.com/

如果你想使用MSDN,请看这里:http://msdn.microsoft.com/en-us/library/System.IO.File_methods(v=vs.110).aspx

剧透警报,我会这样做:

    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
    }
Run Code Online (Sandbox Code Playgroud)


Gur*_*ron 5

只是将所有行收集到我将使用的数组中

var lines = File.ReadAllLines(path);
Run Code Online (Sandbox Code Playgroud)