File in C#
In C# , the File class from the System.IO namespace provides static methods for various file operations. Let’s explore some key aspects of working with files in C#: Creating a File : To create a new file, you can use the File.CreateText(path) method. Here’s an example that checks if a file exists, and if not, creates a new file and writes to it: C# using System; using System.IO; class Test { public static void Main () { string path = @"c:\temp\MyTest.txt" ; if (!File.Exists(path)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine( "Hello" ); sw.WriteLine( "And" ); sw.WriteLine( "Welcome" ); } } // Open the file to read from. using (StreamReader sr = File.OpenText(path)) { string s; while ((s ...