简体   繁体   中英

Putting each line from a text file into an array C#

I am working on selecting a text file with a folder pathway via a Windows form in C# and gathering information on each pathway. At the minute, I can import the file and display only the second pathway in the text file, but no information on the folder. Here is the code I have:

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);
            }
        }
    }

I was thinking that copying each line to a variable using a foreach or putting each line into an array and looping through it to gather the information.

Can anyone advise me which would be most suitable so I can go to MSDN and learn for myself, because, I'd prefer to learn it instead of being given the code.

Thanks!

I am not sure what your question is since you seemed to have answered it. If you want us to review it you question would be better suited to Code Review: https://codereview.stackexchange.com/

If you want to use MSDN look here: http://msdn.microsoft.com/en-us/library/System.IO.File_methods(v=vs.110).aspx

Spoiler alert, here is how I would do it:

    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
    }

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

var lines = File.ReadAllLines(path);

If you want to have more reference rather than the answer itself, take these links one by one all of them explaining things in different manner.

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#)

Hope it will help you to learn more about File IO operations in C#.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM