Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want make a form to input data using four textbox
and by clicking on button want to concate all four data using '|' operator.
like 1|abcd|gfd|989898 and want to save this string in to text file in new line

for example,
text file data....(data.txt)
1|abcd|ahm|989898
2|efgh|vad|545465
3|jhfd|jhf|356242


now if i want to insert 4th record like 4|dfdf|gfd|545421
in a new line...

please give me a proper solution....
Posted
Updated 2-Jan-12 7:21am
v2

1 solution

One way is to create a stringbuilder and use it to create the string. Then use AppendAllText[^] to create the file. For example:
C#
StringBuilder sb = new StringBuilder();

sb.AppendFormat("{0}|{1}|{2}|{3}", "text 1", "text 2", "text 3", "text 4").AppendLine();
try {
   System.IO.File.AppendAllText(@"C:\temp\test.csv", sb.ToString());
} catch (System.Exception ex) {
}
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 2-Jan-12 13:54pm    
A valid solution so have a five!
I'd still prefer String.Format in this case with a new line literal at the end of the format string though!

Cheers!
Wendelius 2-Jan-12 13:57pm    
Thanks :) Yep, <codestring.format< code=""> is also a perfectly viable solution. Happy New Year! :)
Sergey Alexandrovich Kryukov 2-Jan-12 23:02pm    
I hope you don't forget this literal is system-dependent.
Happy New Year!
--SA
Manfred Rudolf Bihy 3-Jan-12 8:33am    
A good point you made there, SA and a happy new year to you too!
PKriyshnA 3-Jan-12 10:08am    
thnx Mr.Mika Wendelius :)

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