फ़ाइल C# में बाहर निकलती है या नहीं, यह जाँचने के लिए C# में File.exists विधि का उपयोग करें।
सबसे पहले, जांचें कि फ़ाइल वर्तमान निर्देशिका में मौजूद है या नहीं।
if (File.Exists("MyFile.txt")) {
Console.WriteLine("The file exists.");
} उसके बाद जांचें कि फ़ाइल निर्देशिका में मौजूद है या नहीं।
if (File.Exists(@"D:\myfile.txt")) {
Console.WriteLine("The file exists.");
} C# में कोई फ़ाइल मौजूद है या नहीं, यह जांचने के लिए आइए पूरा उदाहरण देखें।
उदाहरण
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!");
}
}
} आउटपुट
File does not exist in the current directory! File does not exist in the D directory!