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

时间:2013-12-18 13:34:53

标签: 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;

        //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 个答案:

答案 0 :(得分: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
    }

答案 1 :(得分:5)

将所有行聚集到数组中我将使用

var lines = File.ReadAllLines(path);

答案 2 :(得分:2)

如果你想要更多的参考而不是答案本身,请逐一采用这些链接,所有这些链接都以不同的方式解释。

C# File.ReadLines

How to: Read From a Text File (C# Programming Guide)

How to: Read a Text File One Line at a Time (Visual C#)

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