Click here to Skip to main content
15,850,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I change string[] lines into output string?
protected void Button1_Click(object sender, EventArgs e)
            string data1 = TextBox1.Text;
            using (System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/data.txt"), System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read, 8, System.IO.FileOptions.None))
                byte[] data = System.Text.Encoding.ASCII.GetBytes(data1);
                fs.Write(data, 0, data.Length);

        protected void Button2_Click(object sender, EventArgs e)
            string[] lines = File.ReadAllLines(Server.MapPath("~/data.txt"));
            string stglines
            ???change string[] to strlines
            TextBox2.Text = strline.ToString();

Why not use ReadAllText(String) instead?
Share this answer
teledexterus 6-Jan-15 15:45pm    
How do you use ReadAllText as a string and not a string[]?
PIEBALDconsult 6-Jan-15 15:50pm    
It just is.
CPallini 6-Jan-15 15:53pm    
The way he suggested, namely:
string stglines = File.ReadAllText(Server.MapPath("~/data.txt"));
CPallini 6-Jan-15 15:53pm    
teledexterus 6-Jan-15 15:59pm    
If you do not use the "[]" File.ReadAllText does not work. I need to convert string[] to string.
All you need to do is to loop through the collection of lines using StringBuilder class[^].

StringBuilder sb = new StringBuilder();
foreach (string str in lines)
TexBox2.Text  = sb.ToString();

But, personally, i'd suggest to use solution 1.

String.Concat() method[^] concatenates the elements of a specified String array.

Thank you, PIEBALDconsult. I forgot about that method!
Share this answer
PIEBALDconsult 6-Jan-15 18:08pm    
Why not just String.Concat?
Maciej Los 6-Jan-15 18:17pm    
Good idea!

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