Mega Code Archive

 
Categories / C# / File Stream
 

File Create Creates or overwrites a file in the specified path

using System; using System.IO; using System.Text; class Test {     public static void Main()     {         string path = @"c:\MyTest.txt";         try         {             if (File.Exists(path))             {                 File.Delete(path);             }             using (FileStream fs = File.Create(path))             {                 Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");                 fs.Write(info, 0, info.Length);             }             using (StreamReader sr = File.OpenText(path))             {                 string s = "";                 while ((s = sr.ReadLine()) != null)                 {                     Console.WriteLine(s);                 }             }         }         catch (Exception Ex)         {             Console.WriteLine(Ex.ToString());         }     } }