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