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

3

我正在使用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学习,因为我更愿意学习而不是被给予代码。

谢谢!

3个回答

8
我不确定您的问题是什么,因为您似乎已经回答了它。如果您想让我们审查它,那么您的问题最好适合提交到Code Review: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
    }

5

如果只是要将所有行收集到数组中,我会使用以下方法:

var lines = File.ReadAllLines(path);

2

@ GuruStron 感谢您的支持。这真的有助于改善我的答案。 - Anoop

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接