C# System.IO.File.Exists 在 Unity 中不起作用

标签 c# windows unity-game-engine system.io.file

我正在尝试在我的计算机上找到一个文件,以便我可以将游戏的登录数据存储在该特定文件上。我有一个包含路径的字符串。

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

然后我稍后会有这个:

if (System.IO.File.Exists(path))
{
Debug.Log("Path exists on this computer");
}

else
{
Debug.LogWarning("Path does NOT exist on this computer");
}

我也尝试过更换这个:

else
{
Debug.LogWarning("Path does NOT exist on this computer");
}

这样:

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

但是每次它都会记录错误。所以我不知道该怎么办。似乎其他人也有同样的问题。谢谢,如果你有答案。

最佳答案

Documents 是一个目录,而不是文件,因此不要检查文件,而是检查目录如:

if(File.Exists(path))
{
    // This path is a file
    ProcessFile(path);
}
else if(Directory.Exists(path))
{
    // This path is a directory
    ProcessDirectory(path);
}

请记住,如果您要搜索文件,您的路径应具有如下文件名和扩展名:

public string path = @"C:/Users/DevelopVR/Documents/MyFile.txt";

关于C# System.IO.File.Exists 在 Unity 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63487134/

相关文章:

c# - SQLite 连接策略

windows - Powershell 设置盖子关闭 Action

c++ - 获取 Windows 句柄的 WNDPROC

android - (UNITY3d android 游戏)当我不触摸屏幕时,我的 fps 比我触摸时低

c# - 通过Unity引用python脚本

c# - 使用接口(interface)获取列名时如何使用表达式动态构建LINQ查询?

c# - 如果第二个或第三个表为空,LINQ Join 不返回结果

android - 无法在 Unity Activity 中返回结果

c# - 使用菜单项而不是导航 url 添加按钮单击事件

c - SDL+OpenGL 在 linux 下工作但在 windows 下不工作,不是编译器/链接器问题