I was wondering if anyone knows how to print out the contents of an array into a text file(i.e myfile.txt)

What i have is an array full of strings and my goal is to print each of them onto a seperate line in a text file.

I kno how to write single lines in a text file but not contents of an array.

Any help is greatly appreciated!!!

// given array and filename
string[] array = { "apple", "banana", "cherry" };
string filename = @"C:\Temp\arrayfile.txt";

// short version
using (StreamWriter writer = new StreamWriter(filename))
{
    foreach (string item in array)
        writer.WriteLine(item);
}

// short, short version
File.WriteAllLines(filename, array);

It's a bit off-topic but could you please explain what the "@" is for in:
string filename = @"C:\Temp\arrayfile.txt";

I believe I've done similar things without the @ and it works as it should but I've also seen many people using it pretty often, what exactly does it do?
Thanks.

It's a bit off-topic but could you please explain what the "@" is for in:
string filename = @"C:\Temp\arrayfile.txt";

I believe I've done similar things without the @ and it works as it should but I've also seen many people using it pretty often, what exactly does it do?
Thanks.

If i am right im pretty sure it means
"take anything after this and between the quotes as a literal string"
meaning:

anything between the quotes will be taking and used AS IS

@"C:\documents\test.txt"-----means the computer will store it as (C:\documents\test.txt)

If you use this you dont have to use the \\ anymore

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.