3

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!

3 Answers 3

9

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
    }
Sign up to request clarification or add additional context in comments.

Comments

5

to just gather all lines into array i would use

var lines = File.ReadAllLines(path);

Comments

2

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

1 Comment

@ GuruStron Thanks for your support. It really helps to improve my answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.