Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
thatraja 15-Nov-10 9:23am    
I mentioned in my answer like "Use Stringbuilder with below example"
thatraja 15-Nov-10 9:32am    
Hi, check my updated answer for sample code
Little Daffodil 15-Nov-10 9:35am    
ok, i need to understand the stringbuilder 1st.. ill get back to this post later.
thanks thatraja!

C#
public static string MyMethod(string filename)
{
    string result = string.Empty;
    try
    {
        string[] lines = File.ReadAllLines(filename);
        result = "Your number is " + lines[0] + " and your port is " + lines[1] + ".";
    }
    catch (FileNotFoundException e)
    {
        // Deal with it!
    }
    catch (IndexOutOfRangeException e)
    {
        // Deal with it too! (there aren't two lines in the file)
    }
    // Catch other possible Exceptions here!
    return result;
}
 
Share this answer
 
Use Stringbuilder with below example...that's all

C# Read Text File Line-by-Line[^]

Stringbuilder sample code
C#
StringBuilder sb = new StringBuilder();
int count = lines1.Length;
for (int i = 0; i < count; i++)
{
string line = lines1[i];
sb.AppendLine(line);
}
Console.WriteLine(sb.ToString());
 
Share this answer
 
v2
Comments
thatraja 15-Nov-10 9:21am    
[Comment from OP][Moved from Answer]
Thanks thatraja.
But the code only displays both lines..

Now, I want to use the string and concatenate it. How do I do that?

Thanks.
Little Daffodil 18-Nov-10 8:46am    
thatraja, this is what I'm trying to do:

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


}


However, it's not working. MessageBox does not appear the comport and phone number. :-(
My main problem now is how do I assign each line read from the file as a string variable.. Please help.

Thanks.

(btw, from my reading, i still dont quite get the concept of stringbuilder :-( )
Little Daffodil 18-Nov-10 8:55am    
I've managed to get what I want. This is my code:

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]);
}
thatraja 18-Nov-10 9:16am    
Put the message box inside the loop. Create 2 variables like sNumber, sPort assign the array values to these variables then concatenate with your string. That's all.
Actually I shouldn't confused you by including the string-builder in this issue, apologize.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900