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

橙三吉。 提交于 2019-12-13 09:41:28

问题


I want to find the file in the path known and if it is pre-existing in android then trigger an event(enable/disable canvas) with it. Here is an example I used -

public void FileChk(){

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

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

what am I doing wrong here and how do I get this event to trigger when the file exists.


回答1:


You can use the System.IO namespace.

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
    }
}

The method bool System.IO.File.Exists(string fileName) returns a value indicating if the file exists or not.




回答2:


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

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



回答3:


First of all make sure you imported the System.IO; in the beginning of your script, then just be sure to not write a code like this :

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

instead, make sure to write your code like this (in your case that you want to check for a file):

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

Just REMEMBER:
Directory.Exists checks for folder and File.Exists checks for file.



来源:https://stackoverflow.com/questions/42770467/how-to-check-a-file-exist-or-not-in-unity-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!