An array of streams in C#


Set the string array for the values −

string[] names = new string[] {"Jack", "Tom"};

Now using foreach array, write the content in the file −

using (StreamWriter sw = new StreamWriter("names.txt")) {

   foreach (string s in names) {
      sw.WriteLine(s);
   }
}

The following is an example showing an array of streams to write text to a file −

Example

using System;
using System.IO;

namespace FileApplication {
   class Program {
      static void Main(string[] args) {
         string[] names = new string[] {"Jack", "Tom"};

         using (StreamWriter sw = new StreamWriter("names.txt")) {

            foreach (string s in names) {
               sw.WriteLine(s);
            }
         }

         // Read and show each line from the file.
         string line = "";
         using (StreamReader sr = new StreamReader("names.txt")) {
            while ((line = sr.ReadLine()) != null) {
               Console.WriteLine(line);
            }
         }
         Console.ReadKey();
      }
   }
}

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 21-Jun-2020

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements