string filePath = @"C:\Projects\example.txt"; string fileName = Path.GetFileName(filePath); Console.WriteLine(fileName);
string filePath = @"C:\Projects\example.txt"; if (File.Exists(filePath)) { Console.WriteLine("File exists!"); } else { Console.WriteLine("File does not exist!"); }
string filePath = @"C:\Projects\newfile.txt"; if (!File.Exists(filePath)) { using (StreamWriter sw = File.CreateText(filePath)) { sw.WriteLine("This is a new file!"); } Console.WriteLine("File created successfully."); } else { Console.WriteLine("File already exists!"); }This example uses the `File.CreateText()` method to create a new file at the specified path, and then writes a line of text to the file. If the file does not already exist, it will be created. The output will be "File created successfully." Package library: This is also a built-in class from the System.IO namespace, so it does not require any external library.