如何统一检查文件是否存在android?

How to check a file exist or not in unity android?

我想在已知路径中找到该文件,如果它预先存在于 android 中,则用它触发一个事件(enable/disable canvas)。这是我使用的示例 -

public void FileChk(){

    string filePath = "file://" + Application.temporaryCachePath + "/" + "folder23" + "/" + fileName;

    if (!fileName.Exists)
    {
        //event
    }   
    else
    {       
        //event     
    }
}

我在这里做错了什么以及如何在文件存在时触发此事件。

File f = new File(this.context.getFilesDir(), "catalogAsset" + this.pk + ".jpeg");

if (f.exists()){
  //EXISTS TODO SOMETHING.
} else {
  //NOT EXISTS TODO SOMETHING.
}

您可以使用 System.IO 命名空间。

public void FileChk()
{
    string filePath = "file://" + Application.temporaryCachePath + "/" + "folder23" + "/" + fileName;

    if (System.IO.File.Exists(filePath))
    {
        // The file exists -> run event
    }
    else
    {
        // The file does not exist -> run event
    }
}

方法 bool System.IO.File.Exists(string fileName) returns 指示文件是否存在的值。

首先确保你在脚本的开头导入了System.IO;,然后确保不要写这样的代码:

if (!Directory.Exists(Application.persistentDataPath) + "filename.extention") {}

相反,请确保像这样编写代码(在您想要检查文件的情况下):

if (!File.Exists(Application.persistentDataPath + "/filename.extention")) {}

请记住:
Directory.Exists 检查文件夹,File.Exists 检查文件。