How would i go about printing out the contents of an array into a text file.
I know how to print single lines into a text file but not the contents of an entire array.
Any help is greatly appreciated.
Printable View
How would i go about printing out the contents of an array into a text file.
I know how to print single lines into a text file but not the contents of an entire array.
Any help is greatly appreciated.
By "print to text file" do you mean to save the text into the file? Try look here.
Yes, I would want to print each element of the array as a different line in a text file.
i.e--- array lines = {"Each item","in this","array will","be printed out"," as a different line"}
so in the text file this would print out to....each item would be a different line in it...
could it be as simple as like a foreach loop for each item in the array?
The easiest way (I think) is to create a string and use the System.IO.File.WriteAllText method:
Code:string contents = "";
foreach(string line in lines)
contents += line + Environment.Newline;
File.WriteAllText(path, s, Encoding.Default);
Thanks!
Its actually a little easier this way i figured out:
string[] lines = {"each","word","is","a","different","line"};
File.WriteAllLines(@"c:\ttt.txt", lines, Encoding.Default);
you dont even need a foreach loop just "File.WriteAllLines" and then the name of the array
You're right :) I'm just not used to having my data arranged in an array beforehand :thumb: