वैल्यूज़ के लिए स्ट्रिंग ऐरे सेट करें:
string[] names = new string[] {"Jack", "Tom"};अब foreach ऐरे का उपयोग करके कंटेंट को फाइल में लिखें:
using (StreamWriter sw = new StreamWriter("names.txt")) { foreach (string s in names) { sw.WriteLine(s); } }निम्नलिखित एक उदाहरण है जो टेक्स्ट को फाइल में लिखने के लिए स्ट्रीम्स के ऐरे को दर्शाता है:
उदाहरण
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(); } } }