Hello, I need to read from a text file.

There are only 2 lines in the text file. And I need to read both, line by line as a string. After reading it, I want to use it (concatenate).

For example, each line is represented as string1 and string2. I will concatenate to make a sentence like

"Your number is " + string1 + " and your port is " + string2".

My question is, how do I do it? I read it as array? And convert it to string? Or vice versa?

I'm really confused.

Thanks in advance.

using (StreamReader sr = File.OpenText("filenamehere")) {
    String input;
    while ((input = sr.ReadLine()) != null) {
        //do stuff with line
    }
}

If you are going to be concatenating text, use StringBuilder to do it.

Thanks Momerath. However, from my reading, StringBuilder is to be used if i use any iteration/loop right?

What I'm confused now, how do I save each line as a string?
I mean, when the compiler reads the 1st line, how does it save it as string1 and so on?

using (StreamReader sr = File.OpenText(@"C:config.txt"))
                {
                    String input;
                    string[] lines = new string[2];
                    while ((input = sr.ReadLine()) != null)
                    {
                        int i;
                        if ((input = sr.ReadLine()) != null)
                        {
                            for (i = 0; i < 2; i++)
                            {
                                input = lines[i];
                            }
                            MessageBox.Show("COM Port is " + lines[0]);
                            MessageBox.Show("Phone number is " + lines[1]);
                        }
                    }

                    
                }

This is what I meant of what I'm trying to do. However, the messagebox does not add the string :-(

Can somebody help me?

String[] lines = File.ReadAllLines(@"C:\config.txt");
MessageBox.Show("COM Port is " + lines[0]);
MessageBox.Show("Phone number is " + liens[1]);

or line by line

using (StreamReader sr = File.OpenText(@"C:\config.txt")) {
    String[] lines = new String[2];
    for (int i = 0; i < 2; i++) {
        lines[i] = sr.ReadLine();
    }
    MessageBox.Show("COM Port is " + lines[0]);
    MessageBox.Show("Phone number is " + lines[1]);
}

I don't see that you are ever assigning anything to "lines". I see no way this would actually work. The first response that was given is exactly what I use.

If you want lines to have something assigned you must assign something to them before trying to use them.

I hope this was helpful.

hello momerath and xpartmgr..

using (StreamReader sr = File.OpenText(@"C:\config.txt"))
                {

                    String input;
                    string[] lines = File.ReadAllLines(@"C:\config.txt");

                    while ((input = sr.ReadLine()) != null)
                    {
                        int i;
                        // if ((input = sr.ReadLine()) != null)
                        for (i = 0; i < 2; i++)
                            {
                                input = lines[i];
                            }
                    }
                    MessageBox.Show("COM Port is " + lines[0]);
                    MessageBox.Show("Phone number is " + lines[1]);
                 }

Above is the code that I used, finally it worked! I don't know how to explain. That's what I got from my trial and error, following few examples on other forum.

that doesnt make sense

while ((input = sr.ReadLine()) != null)
                    {
                        int i;
                        // if ((input = sr.ReadLine()) != null)
                        for (i = 0; i < 2; i++)
                            {
                                input = lines[i];
                            }
                    }

does nothing. you are assigning input from your string array but then dont use it. in the end you just use the string array to display anyway.

Yes divin757, I just get rid of that particular part, and still it does what I want.
Thanks a lot.
You guys are helpful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.