CSharp examples for File IO:File
File Write With Using
using System;// w ww . j a v a 2s . co m using System.IO; public class Program { public static void Main(string[] args) { string fileName = ""; while (true) { try { Console.Write("Enter filename " + "(Enter blank filename to quit):"); fileName = Console.ReadLine(); if (fileName.Length == 0) { break; } FileStream fs = File.Open(fileName,FileMode.CreateNew,FileAccess.Write); using (StreamWriter sw = new StreamWriter(fs)) { Console.WriteLine("Enter text; enter blank line to stop"); while (true) { string input = Console.ReadLine(); if (input.Length == 0) { break; } sw.WriteLine(input); } } } catch (IOException ioErr) { string dir = Directory.GetCurrentDirectory(); string path = Path.Combine(dir, fileName); // System.IO.Path class. Console.WriteLine("Error on file {0}", path); Console.WriteLine(ioErr.Message); } } } }