如何使用 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!

更新時間:22-Jun-2020

76 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始學習
廣告