首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> ASP.NET 基礎

ASP.NET 檢查伺服器檔案與目錄是否存在的標準做法

     編輯:ASP.NET 基礎

前言

在 ASP.NET 開發中,經常需要驗證伺服器端的檔案或目錄是否存在。.NET Framework 提供了 `System.IO.File.Exists` 與 `System.IO.Directory.Exists` 兩個靜態方法,使用簡單且效能良好。本文說明正確用法與路徑處理注意事項。

一、檢查檔案是否存在

基本語法

using System.IO;

string filePath = @"C:\Data\aaa.txt"; // 使用 @ 字串避免跳脫字元問題

if (File.Exists(filePath))
{
    // 檔案存在,可進行讀取、複製、刪除等操作
    Console.WriteLine("檔案存在:" + filePath);
}
else
{
    Console.WriteLine("檔案不存在");
}

路徑處理關鍵點

  • 使用 `@` 逐字字串:`@"C:\Data\file.txt"` 避免雙反斜線 `\\`
  • Server.MapPath:Web 專案中將虛擬路徑轉為實體路徑
    string physicalPath = Server.MapPath("~/Uploads/file.txt");
  • Path.Combine:安全組合路徑片段
    string fullPath = Path.Combine(baseDir, "subfolder", fileName);

二、檢查目錄是否存在

基本語法

using System.IO;

string dirPath = @"C:\WebSites\www.jb51.net";

if (Directory.Exists(dirPath))
{
    Console.WriteLine("目錄存在:" + dirPath);
    
    // 可進一步列舉檔案
    string[] files = Directory.GetFiles(dirPath, "*.txt");
}
else
{
    Console.WriteLine("目錄不存在");
    // 如需建立:Directory.CreateDirectory(dirPath);
}

三、Web 專案完整範例

情境:上傳檔案前檢查目標資料夾,不存在則自動建立

public bool SaveUploadedFile(HttpPostedFileBase postedFile, string relativeFolder)
{
    // 1. 轉換為實體路徑
    string physicalFolder = Server.MapPath(relativeFolder);
    
    // 2. 確保目錄存在
    if (!Directory.Exists(physicalFolder))
    {
        try
        {
            Directory.CreateDirectory(physicalFolder);
        }
        catch (Exception ex)
        {
            // 記錄錯誤:權限不足、路徑無效等
            LogError("建立目錄失敗:" + physicalFolder, ex);
            return false;
        }
    }
    
    // 3. 組合完整檔案路徑
    string fileName = Path.GetFileName(postedFile.FileName);
    string fullPath = Path.Combine(physicalFolder, fileName);
    
    // 4. 避免覆蓋:檔案存在則重新命名
    if (File.Exists(fullPath))
    {
        string name = Path.GetFileNameWithoutExtension(fileName);
        string ext = Path.GetExtension(fileName);
        int counter = 1;
        while (File.Exists(fullPath))
        {
            fullPath = Path.Combine(physicalFolder, $"{name}_{counter}{ext}");
            counter++;
        }
    }
    
    // 5. 儲存檔案
    try
    {
        postedFile.SaveAs(fullPath);
        return true;
    }
    catch (Exception ex)
    {
        LogError("儲存檔案失敗:" + fullPath, ex);
        return false;
    }
}

四、常見陷阱與最佳實務

情境錯誤做法正確做法
路徑字串`"C:\Folder\file.txt"``@"C:\Folder\file.txt"` 或 `"C:\\Folder\\file.txt"`
Web 相對路徑直接使用 `"~/Uploads/"``Server.MapPath("~/Uploads/")`
組合路徑字串連接 `folder + "\\" + file``Path.Combine(folder, file)`
權限問題不處理例外try-catch 捕捉 UnauthorizedAccessException
競爭條件檢查後直接操作操作時再用 try-catch 處理「不存在」例外

五、.NET Core / .NET 5+ 現代化寫法

using System.IO;

// FileInfo / DirectoryInfo 提供更豐富的屬性
var fileInfo = new FileInfo(physicalPath);
if (fileInfo.Exists)
{
    long size = fileInfo.Length;
    DateTime modified = fileInfo.LastWriteTime;
}

// 非同步檢查(適用於高併發)
// 注意:IO 作業本質同步,通常不需非同步包裝
bool exists = await Task.Run(() => File.Exists(path));

總結

使用 `File.Exists` 與 `Directory.Exists` 是最直接的檢查方式。關鍵在於正確處理路徑轉換(`Server.MapPath`)、路徑組合(`Path.Combine`)與權限例外。實務上建議「嘗試操作並捕捉例外」取代「先檢查再操作」,避免競爭條件。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved