C# में File.Exists Method का उपयोग करके Check करें कि File Exist करती है या नहीं।
सबसे पहले, Check करें कि File Current Directory में Present है या नहीं।
if (File.Exists("MyFile.txt")) { Console.WriteLine("The file exists."); }उसके बाद Check करें कि File किसी Directory में Exist करती है या नहीं।
if (File.Exists(@"D:\myfile.txt")) { Console.WriteLine("The file exists."); }आइए C# में File Exist Check करने का Complete Example देखते हैं।
Example
using System; using System.IO; class Demo { static void Main() { if (File.Exists("MyFile.txt")) { Console.WriteLine("File exists..."); } else { Console.WriteLine("File does not exist in the current directory!"); } if (File.Exists(@"D:\myfile.txt")) { Console.WriteLine("File exists..."); } else { Console.WriteLine("File does not exist in the D directory!"); } } }Output
File does not exist in the current directory! File does not exist in the D directory!