You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中System.IO.File.Exists无法正常工作的问题求助

Fixing Your Path Existence Check Issue

Hey there! I see exactly what's going on here—you're hitting a super common gotcha with .NET's file system checks. Let's break it down and get this fixed:

The Root Problem

You’re using System.IO.File.Exists() to verify that your Documents folder exists, but this method is built to check for files, not directories. No matter if your Documents folder is actually present or not, File.Exists() will always return false for a folder path. That’s why your warning keeps popping up every time you run the code.

The Simple Fix

Swap out File.Exists() with System.IO.Directory.Exists()—this is the correct method to check if a folder path exists. Here’s your updated code:

public string path = "C:/Users/DevelopVR/Documents"; 

if (System.IO.Directory.Exists(path)) 
{
    Debug.Log("Path exists on this computer"); 
} 
else 
{
    Debug.LogWarning("Path does NOT exist on this computer"); 
}

A Couple Extra Tips

  • That else if (!System.IO.File.Exists(path)) version you tried is totally redundant. The else clause already covers all cases where the initial if condition fails, so adding that extra check doesn’t change anything (plus it still uses the wrong method!).
  • If you later need to check for a specific file inside that Documents folder (like your game’s login data file), use Path.Combine() to safely merge the folder path with the filename (this handles slashes correctly across different operating systems) and then use File.Exists() on the full file path:
    string loginDataPath = System.IO.Path.Combine(path, "game_login_data.txt");
    if (System.IO.File.Exists(loginDataPath))
    {
        Debug.Log("Login data file found!");
    }
    else
    {
        Debug.LogWarning("Login data file missing.");
    }
    

That should resolve your issue once and for all!

内容的提问来源于stack exchange,提问作者GraniteSOS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 21:02:57