How to use join method to combine several lines from a file into one line?

I am new to C #, so bear with me. I have a text file that I upload with a single column of numbers. I can upload the file and run them through the code below, but how can I put them on one line?

file data (each number on the separte line, carriage return); 12345 54321 22222

final result; '12345', '54321', '22222'

private void button1_Click(object sender, EventArgs e)
{
        int counter = 0;
        string line;
        ArrayList combine = new ArrayList();

        // Read the file and display it line by line.
        System.IO.StreamReader file =
           new System.IO.StreamReader("c:\\test.txt");
        while ((line = file.ReadLine()) != null)
        {
            Console.WriteLine(line);
            counter++;
            //just running through so i can see what it retrieving
            MessageBox.Show(line);
        }

        file.Close();

        // Suspend the screen.
+3
source share
2 answers

Why don't you just use File.ReadAllText . It reads all the lines and returns them as one line.

+2
source

File.ReadAllLines, , String.Join, . :

string[] lines = File.ReadAllLines("C:\\test.txt");
string join = String.Join(" ", lines);

join , , .
, , String.Join (, , , String.Join(", ", lines)). .

+1

All Articles