如何使用 C# 檢查檔案是否存在?
假設我們需要查詢以下檔案 −
E:
ew.txt
要檢查上述檔案是否存在,請使用 Exists() 方法–
if (File.Exists(@"E:
ew.txt")) { Console.WriteLine("File exists..."); }
以下是檢查檔案是否存在所需的完整程式碼 −
示例
using System; using System.IO; public class Demo { public static void Main() { if (File.Exists(@"E:
ew.txt")) { Console.WriteLine("File exists..."); } else { Console.WriteLine("File does not exist in the E directory!"); } } }
輸出
File does not exist in the E directory!
廣告